为了账号安全,请及时绑定邮箱和手机立即绑定

用子数组展平数组

用子数组展平数组

守候你守候我 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 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信