-1

I have this structure below, and I want to loop through the hierarchy without missing any object.

{
  "countries": [
    {
      "name": "Denmark",
      "id": "APA1",
      "children": [
        {
          "name": "Zealand",
          "id": "APA1.1",
          "parentId": "APA1",
          "children": [
            {
              "name": "Copenhagen",
              "id": "APA1.1.1",
              "parentId": "APA1.1",
              "children": [
                {
                  "name": "Dublin",
                  "id": "ANA1",
                  "parentId": "APA1.1.1.1",
                  "hostNames": [
                    {
                      "ip": "20.190.129.1"
                    },
                    {
                      "ip": "20.190.129.2"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "name": "Jutland",
          "id": "APA1.2",
          "parentId": "APA1",
          "children": [
            {
              "name": "Nordjylland",
              "id": "APA1.2.1",
              "parentId": "APA1.2",
              "children": [
                {
                  "name": "Aalborg",
                  "id": "APA1.2.1.1",
                  "parentId": "APA1.2.1",
                  "children": [
                    {
                      "name": "Risskov",
                      "id": "ANA3",
                      "parentId": "APA1.2.1.1",
                      "hostNames": [
                        {
                          "ip": "40.101.81.146"
                        }
                      ]
                    },
                    {
                      "name": "Brabrand",
                      "id": "ANA4",
                      "parentId": "APA1.2.1.1",
                      "hostNames": [
                        {
                          "ip": "43.203.94.182"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

The reason why I want to loop through the hierarchy is that I want to turn this into a flat structure. So essentially I'm gonna take every object and move it to another array which has the structure that I want. I just want to know how to access the children.

The wanted result:

"applicationGroups": [
    {
        "id" : "APA1",
        "name": "Denmark",
    },
    {
        "name": "Zealand",
        "id": "APA1.1",
        "parentId": "APA1"
    },
    {
        "name": "Copenhagen",
        "id": "APA1.1.1",
        "parentId": "APA1.1"
    },
    {
        "name": "Dublin",
        "id": "ANA1",
        "parentId": "APA1.1.1.1"
    },
    {
        "name": "Jutland",
        "id": "APA1.2",
        "parentId": "APA1"
    },
    {
        "name": "Nordjylland",
        "id": "APA1.2.1",
        "parentId": "APA1.2"
    },
    {
        "name": "Aalborg",
        "id": "APA1.2.1.1",
        "parentId": "APA1.2.1"
    },
    {
        "name": "Risskov",
        "id": "ANA3",
        "parentId": "APA1.2.1.1"
    },
    {
        "name": "Brabrand",
        "id": "ANA4",
        "parentId": "APA1.2.1.1"
    }
]

I'm a bit new to JavaScript, and I don't really know where to start, but this example that I have given is not identical to the actual one that I'm working on, so I just want the principle so I can implement it myself in my actual code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TheStranger
  • 1,387
  • 1
  • 13
  • 35

3 Answers3

1

You can combine the Array.flat() method and this answer to flatten objects recursively. Using recursive functions is the faster way to accomplish that.

Tasos
  • 1,880
  • 13
  • 19
1

To get a flat structure you could use reduce method to create recursive function.

const data = {"countries":[{"name":"Denmark","id":"APA1","children":[{"name":"Zealand","id":"APA1.1","parentId":"APA1","children":[{"name":"Copenhagen","id":"APA1.1.1","parentId":"APA1.1","children":[{"name":"Dublin","id":"ANA1","parentId":"APA1.1.1.1","hostNames":[{"ip":"20.190.129.1"},{"ip":"20.190.129.2"}]}]}]},{"name":"Jutland","id":"APA1.2","parentId":"APA1","children":[{"name":"Nordjylland","id":"APA1.2.1","parentId":"APA1.2","children":[{"name":"Aalborg","id":"APA1.2.1.1","parentId":"APA1.2.1","children":[{"name":"Risskov","id":"ANA3","parentId":"APA1.2.1.1","hostNames":[{"ip":"40.101.81.146"}]},{"name":"Brabrand","id":"ANA4","parentId":"APA1.2.1.1","hostNames":[{"ip":"43.203.94.182"}]}]}]}]}]}]}

function flat(data) {
  return data.reduce((r, { children, ...rest }) => {
    if (children) r.push(...flat(children))
    r.push(rest)
    return r;
  }, [])
}

const result = flat(data.countries)
console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

You could take a flatMap approach for the recursive call of a flattening callback.

const
    flat = ({ hostNames, children = [], ...o }) => [o, ...children.flatMap(flat)],
    data = { countries: [{ name: "Denmark", id: "APA1", children: [{ name: "Zealand", id: "APA1.1", parentId: "APA1", children: [{ name: "Copenhagen", id: "APA1.1.1", parentId: "APA1.1", children: [{ name: "Dublin", id: "ANA1", parentId: "APA1.1.1.1", hostNames: [{ ip: "20.190.129.1" }, { ip: "20.190.129.2" }] }] }] }, { name: "Jutland", id: "APA1.2", parentId: "APA1", children: [{ name: "Nordjylland", id: "APA1.2.1", parentId: "APA1.2", children: [{ name: "Aalborg", id: "APA1.2.1.1", parentId: "APA1.2.1", children: [{ name: "Risskov", id: "ANA3", parentId: "APA1.2.1.1", hostNames: [{ ip: "40.101.81.146" }] }, { name: "Brabrand", id: "ANA4", parentId: "APA1.2.1.1", hostNames: [{ ip: "43.203.94.182" }] }] }] }] }] }] },
    result = data.countries.flatMap(flat);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392