javascript - 使用旧数组中的两列创建新数组
问题描述
我有一个数组如下:
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
});
编辑
在遵循以下答案之一后,我意识到我忘了提及我还需要将结果过滤为唯一的行。现在我只选择年份和月份列,我得到多个重复的行
解决方案
现在我正在尝试使用 .map() 并且它不起作用
- 因为 newArray 有长度
0
,你正试图映射它 - 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)
推荐阅读
- python-3.x - ImportError:无法从“类型”导入名称“DictType”
- node.js - 如何将 URL 中的数组作为参数传递给 Promise.all
- javascript - 无法为 React Router 链接手动刷新页面或键入 URL
- excel - VBA 将电子表格中的 ID 号匹配到另一张工作表然后更新信息的最佳方法
- jquery - 如何使用jQuery移动一个正方形?用户输入像素
- regex - 使用带有 -regex 选项的 find 实用程序查找以特定字母开头的文件
- mysql - 不需要的 MySQL 表名字符大写
- c++ - 二叉树算子深拷贝
- python - 如何将多行 JSON 文件转换为 Dataframe
- ruby - 如何使用 i% 在一行中创建和分配符号?