1 回答
TA贡献1790条经验 获得超9个赞
您可以采用迭代和递归方法并使用Array#flatMap
.
这种方法只需要entity
对象。
const
getFlat = ({ entity, childNodes }) => [entity, ...childNodes.flatMap(getFlat)],
array = [{ entity: { id: "2387232d-5fc4-4344-b71b-42f70ff16040", pid: "bdba6327-4055-42db-8f2e-34c595a6f79f", name: "Outsourcing Lead", title: "OL" }, childNodes: [{ entity: { id: "91444f59-1b70-4817-a9a5-8e9b85a9799a", pid: "2387232d-5fc4-4344-b71b-42f70ff16040", name: "Corporate Accountant", title: "CA" }, childNodes: [], depth: 2, parent: { id: "2387232d-5fc4-4344-b71b-42f70ff16040", pid: "bdba6327-4055-42db-8f2e-34c595a6f79f", name: "Outsourcing Lead", title: "OL" } }, { entity: { id: "4454e911-d1dc-494d-b243-e3237b9971d7", pid: "2387232d-5fc4-4344-b71b-42f70ff16040", name: "Liaison Officer / Messenger", title: "LO" }, childNodes: [{ entity: { id: "893b5911-eb46-4bb3-a3eb-ac375f959202", pid: "4454e911-d1dc-494d-b243-e3237b9971d7", name: "System Administrator", title: "SYSAD" }, childNodes: [], depth: 3, parent: { id: "4454e911-d1dc-494d-b243-e3237b9971d7", pid: "2387232d-5fc4-4344-b71b-42f70ff16040", name: "Liaison Officer / Messenger", title: "LO" } }], depth: 2, parent: { id: "2387232d-5fc4-4344-b71b-42f70ff16040", pid: "bdba6327-4055-42db-8f2e-34c595a6f79f", name: "Outsourcing Lead", title: "OL" } }], depth: 1, parent: null }],
flat = array.flatMap(getFlat);
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
添加回答
举报