用子数组展平数组

我有一个看起来像这样的数组


[

{

    "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);

“数据”变量是我的数组。但它不起作用。

感谢任何帮助。谢谢。


守候你守候我
浏览 109回答 1
1回答

富国沪深

您可以采用迭代和递归方法并使用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; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript