从javascript数组中删除嵌套的null值

时间:2019-04-16 21:11:02

标签: javascript arrays multidimensional-array

我的javascript数组如下。我想删除所有子数组中的所有空值。我设法删除如下。但是我正在寻找除此以外的更优雅的解决方案

let data = [
            {
                "id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
                "name": "organization 1",
                "type": "org",
                "title": "organization 1",
                "children": [
                    null,
                    {
                        "id": "6571cada-490c-41db-97e8-197a9c0faabb",
                        "name": "location 3",
                        "org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
                        "type": "location",
                        "title": "location 3",
                        "children": [
                            null,
                            {
                                "id": "8620fce9-f7d0-442a-86e8-f58e9029a164",
                                "name": "zone 3",
                                "zone_settings_id": null,
                                "location_id": "6571cada-490c-41db-97e8-197a9c0faabb",
                                "type": "zone",
                                "title": "zone 3",
                                "children": [
                                    null,
                                    null,
                                    null
                                ]
                            },
                            null,
                            null
                        ]
                    },
                    {
                        "id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
                        "name": "location 4",
                        "org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
                        "type": "location",
                        "title": "location 4",
                        "children": [
                            null,
                            null,
                            {
                                "id": "db14daf4-4488-47fa-8d18-2d213b3a54a5",
                                "name": "zone 4",
                                "zone_settings_id": null,
                                "location_id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
                                "type": "zone",
                                "title": "zone 4",
                                "children": [
                                    null,
                                    null,
                                    {
                                        "id": "6ae5b04a-1101-4d73-80e4-05d4db454406",
                                        "gwId": "E4956E45107R",
                                        
                                        "zone_id": "db14daf4-4488-47fa-8d18-2d213b3a54a5",
                                        
                                        "org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
                                        
                                        "title": "E4:95:6E:45:10:7R"
                                    }
                                ]
                            },
                            {
                                "id": "c01398c6-7650-426b-936d-6b88b1b507f2",
                                "name": "zone 5",
                                "zone_settings_id": null,
                                "location_id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
                                "type": "zone",
                                "title": "zone 5",
                                "children": [
                                    null,
                                    null,
                                    null
                                ]
                            }
                        ]
                    },
                    null
                ]
            },
            {
                "id": "46665d49-020d-411f-9f11-c9ddad9a741c",
                "name": "organization 2",
                "type": "org",
                "title": "organization 2",
                "children": [
                    null,
                    null,
                    null,
                    null
                ]
            },
            {
                "id": "95e7d05b-fe67-422d-8617-9f10633ea6f6",
                "name": "org 3",
                "type": "org",
                "title": "org 3",
                "children": [
                    null,
                    null,
                    null,
                    {
                        "id": "0a8509d8-1fd8-486c-8457-3ff393c09abc",
                        "name": "location 1 of org 3",
                        "org_id": "95e7d05b-fe67-422d-8617-9f10633ea6f6",
                        "type": "location",
                        "title": "location 1 of org 3",
                        "children": [
                            null,
                            null,
                            null,
                            null
                        ]
                    }
                ]
            }
        ]


let orgs = data.filter(org => org != null);
            orgs.forEach(org => {
                org.children = org.children.filter(location => location != null);
            })
            orgs.forEach(org => {
                org.children.forEach(loc => {
                    loc.children = loc.children.filter(zone => zone != null);
                })
            })
            orgs.forEach(org => {
                org.children.forEach(loc => {
                    loc.children.forEach(zone => {
                        zone.children = zone.children.filter(router => router != null);
                    })
                })
            })
           console.log(orgs);

1 个答案:

答案 0 :(得分:4)

  const noNull = array => array
    .filter(it => it !== null)
    .map(it => it.children ? { ...it, children: noNull(it.children) } : it);

  const result = noNull(data);

您可以以不变的方式递归过滤数组。

否则变异版本将是:

 const noNull = array => {
    const result = array.filter(it => it !== null);

    for(const value of result)
      if(value.children) value.children = noNull(value.children);

   return result;
 };