interkey
interkey

Reputation: 355

Flattening an array with child arrays

I have an array that looks like this

[
{
    "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
}]

However, I want a result that would look like this.

[
{
    "id": "2387232d-5fc4-4344-b71b-42f70ff16040",
    "pid": "bdba6327-4055-42db-8f2e-34c595a6f79f",
    "name": "Outsourcing Lead",
    "title": "OL"
},
{
    "id": "2387232d-5fc4-4344-b71b-42f70ff16040",
    "pid": "bdba6327-4055-42db-8f2e-34c595a6f79f",
    "name": "Outsourcing Lead",
    "title": "OL"
},
{
     "id": "893b5911-eb46-4bb3-a3eb-ac375f959202",
     "pid": "4454e911-d1dc-494d-b243-e3237b9971d7",
     "name": "System Administrator",
     "title": "SYSAD"
}]

I have searched and tried answers on this threads

Merge/flatten an array of arrays

Flatten a jagged multi dimensional array

var flattened = [].concat.apply([], data);

The "data" variable is my array. But it's not working.

Appreciate any help. Thanks.

Upvotes: 1

Views: 182

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386570

You could take an iterative and recursive approach and return a flat array by using Array#flatMap.

This approach takes only entity objects.

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

Upvotes: 5

Related Questions