2

My javascript array is as below. I want to remove all the null values inside all the children arrays. I managed to remove as below. but i'm looking for more elegant solution other than this

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

2
  • 3
    Your data has no single null in it though ... Commented Apr 16, 2019 at 21:16
  • wrong data has been added. now there are null values Commented Apr 16, 2019 at 21:20

1 Answer 1

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

  const result = noNull(data);

You could recursively filter the arrays in an immutable way.

Or the mutating version would be:

 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;
 };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.