簡體   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