-1

I have the following object:

{
    "models": [
        {
            "_id": "5d4a9c6a9b2de73738c7e15b",
            "children": [
                {
                    "id": "a1",
                    "name": "a1",
                    "children": [
                        {
                            "id": "a2",
                            "name": "a2",
                            "children": [
                                {
                                    "id": "a3",
                                    "name": "a3",
                                    "children": [],
                                    "data": {
                                        "importance": 3
                                    }
                                },
                                {
                                    "id": "a4",
                                    "name": "a4",
                                    "children": [
                                        {
                                            "id": "a5",
                                            "name": "a5",
                                            "children": [],
                                            "data": {
                                                "importance": 4
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "a6",
                    "name": "a6",
                    "children": [
                        {
                            "id": "a7",
                            "name": "a7",
                            "children": [
                                {
                                    "id": "a8",
                                    "name": "a8",
                                    "children": [
                                        {
                                            "id": "a9",
                                            "name": "a9",
                                            "children": [],
                                            "data": {
                                                "importance": 2
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

I want to iterate over the whole object and his child, so i can get the id of last child, in the example above i want to get parent a1 has children a3 and a5, and parent a6 has child a9.

A have arrays with nested objects inside my main object, so i for .. in won't work with me

Ayoub k
  • 7,788
  • 9
  • 34
  • 57
  • here a have arrays i need to loop through also. so *for .. in* wont work – Ayoub k Aug 20 '19 at 09:33
  • I don't know the depth of the object, it's dynamic, so i can't access to the needed field by using indexes and dot notation .. – Ayoub k Aug 20 '19 at 09:44
  • I'm aware this is a duplicate, but I still think this is slightly more complex than the related question so, here is a solution to your problem, hope this helps you to get your job done :) https://jsfiddle.net/p1fLn4h8/ – briosheje Aug 20 '19 at 09:48

1 Answers1

1

You can use a recursive function to get the required result. See the solution below.

const data = {
  "models": [{
    "_id": "5d4a9c6a9b2de73738c7e15b",
    "children": [{
        "id": "a1",
        "name": "a1",
        "children": [{
          "id": "a2",
          "name": "a2",
          "children": [{
              "id": "a3",
              "name": "a3",
              "children": [],
              "data": {
                "importance": 3
              }
            },
            {
              "id": "a4",
              "name": "a4",
              "children": [{
                "id": "a5",
                "name": "a5",
                "children": [],
                "data": {
                  "importance": 4
                }
              }]
            }
          ]
        }]
      },
      {
        "id": "a6",
        "name": "a6",
        "children": [{
          "id": "a7",
          "name": "a7",
          "children": [{
            "id": "a8",
            "name": "a8",
            "children": [{
              "id": "a9",
              "name": "a9",
              "children": [],
              "data": {
                "importance": 2
              }
            }]
          }]
        }]
      }
    ]
  }]
};

function getLastChildren(obj, lastChildren) {
  if (obj.children.length) {
    obj.children.forEach((child) => {
      getLastChildren(child, lastChildren)
    });
  } else {
    lastChildren.push(obj);
  }
}

data.models[0].children.forEach((child) => {
  console.log("Parent ", child.id);
  const children = [];
  getLastChildren(child, children);
  console.log("Children ", children.map(c => c.id).join(", "));
});
Vivek
  • 2,665
  • 1
  • 18
  • 20