我有一个数组如下:
items = [
{"year": 2010, "month": 11, "day":23}
{"year": 2009, "month": 10, "day":15}
{"year": 2009, "month": 10, "day":10} //added after my edit below
]
我想创建一个只有 2 列的新数组,如下所示:
newArarry = [
{"year": 2010, "month": 11}
{"year": 2009, "month": 10}
]
现在我正在尝试使用 .map() 但它不起作用:
const newArray = [];
newArray.map({
year: items.year,
month: items.month
});
编辑
在遵循以下答案之一后,我意识到我忘记提及我还需要将结果过滤为唯一行。现在我只选择年和月列,我得到多个重复行
最佳答案
Right now Im trying with .map() and its not working
- 因为 newArray 的长度为
0
而您正试图在其上进行映射 - map 接受回调,但你传递的是对象
- 不将 map 的输出分配给任何变量
let items = [{"year": 2010, "month": 11, "day":23},{"year": 2009, "month": 10, "day":15}]
let final = items.map(({day,...rest}) => ({...rest}))
console.log(final)
关于javascript - 使用旧数组中的两列创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234027/