繁体   English   中英

如何在 Angular 中将对象数组转换为数组数组?

[英]How to convert Array of Array of Objects to an Array of Arrays in Angular?

[[{ id: 26, type: "Source", name: "Email" }], [{ id: 27, type: "Source", name: "Id" }, { id: 29, type: "Divider", name: "+" }, { id: 30, type: "Source", name: "SupplierId" }], [{ id: 28, type: "Source", name: "CommunityId" }]

如何将上面的对象数组转换为这样的数组数组,其中“名称”被挑出?

[["Email"],["Id","+", "SupplierId"],["CommunityId"]]

我已经尝试像这样映射它:

this.exportColumns = columns.flatMap(obj => obj.sourceColumn).map(obj => obj?.name);

但我得到了这个结果:

[ "Email", "Id", "+", "SupplierId", "CommunityId" ]

在第一层数组中使用第二张地图

data.map(item => item.map(i => i.name));

这是我实现输出的方法。

 const d = [ [{ id: 26, type: "Source", name: "Email" }], [{ id: 27, type: "Source", name: "Id" }, { id: 29, type: "Divider", name: "+" }, { id: 30, type: "Source", name: "SupplierId" }], [{ id: 28, type: "Source", name: "CommunityId" }] ] const newArr = d.reduce((prev, curr) => { if(Array.isArray(curr)) { prev.push(curr.map((p) => p.name)) } else { prev.push(curr.name) // or use this if you want array // prev.push([curr.name]) } return prev; }, []) console.log(newArr)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM