javascript - 使用旧数组中的两列创建新数组

标签 javascript arrays object ecmascript-6

我有一个数组如下:

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/

相关文章:

javascript - 如果我使用 JavaScript 搜索 “Addendum”,如何找到该号码?

.net - Visual Studio 2008 和覆盖现有文件扩展名的语言服务

javascript - 在<select>标签中查找对应的值并显示

java - 将 ArrayList 转换为数组 PayPalItem 时出错

java - 错误 : Code too large

javascript - Lodash 按值对深层对象进行排序和排序,而不丢失 key

javascript - 将对象转换为函数

javascript - 如何屏蔽字符串中的字母和数字

arrays - powershell Get-ChildItem 结果在数组中

javascript - 是否可以从 javascript 对象中读取大小(kb/mb)