亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用子數組展平數組

用子數組展平數組

守候你守候我 2023-04-27 16:42:49
我有一個看起來像這樣的數組[{    "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"                    }                }            ],我已經在這個線程上搜索并嘗試了答案合并/展平數組數組展平鋸齒狀的多維數組var flattened = [].concat.apply([], data);“數據”變量是我的數組。但它不起作用。感謝任何幫助。謝謝。
查看完整描述

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; }


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號