javascript - 如何在javascript中从内部JSON数组创建具有一些属性的数组
问题描述
所以我有 :
list =
{
id: 1,
arr: [
{index : 1 , description: "lol" , author: "Arthur"},
{index : 2 , description: "sdadsa" , author: "Bob"},
{index : 3 , description: "loasd" , author: "Mackenzie"}
]
}
我想只使用 arr 数组中的 description 和 author 属性创建一个数组。
我试过了var a = {l : list.arr.map(x => {x.description,x.author})}
。但是数组中的所有项目都是 undefined 。
解决方案
另一种方法是使用rest 参数。这样,您可以删除索引并保持其他所有内容不变。
var list = {
id: 1,
arr: [
{ index: 1, description: "lol", author: "Arthur" },
{ index: 2, description: "sdadsa", author: "Bob" },
{ index: 3, description: "loasd", author: "Mackenzie" },
],
};
var a = list.arr.map(({index, ...rest}) => rest);
console.log(a);
推荐阅读
- javascript - 映射对象数组
- java - Flink 批处理作业总是由于 AskTimeoutException 或 TaskManager 的心跳与 id 容器超时而失败
- r - 如何重命名以“.csv”结尾的文件夹中的文件??[R]
- javascript - TypeScript 的 `typesafe-actions` 模块中可能存在的错误
- php - 使用PHP将数据从angularjs插入Mysql
- flutter - 在颤动中刷新屏幕的一部分
- angular - 在单个模板中多次使用角度组件 - 在其中附加列表
- r - 无法预测 R 中的问题
- c++ - 为什么 x 成员变量不明确?
- python - 无法识别新的 Python 版本。我安装了新的 python 版本,但是当我尝试安装包时出现错误