0

I have an array of objects that looks like this:

[
    {
        "id": "5eece32be3a4f43ca8e98688",
        "title": "Main",
        "children": [
            {
                "id": "5eece339e3a4f43ca8e98689",
                "title": "Child"
            }
        ]
    },
    {
        "id": "5eece354e3a4f43ca8e9868a",
        "title": "Another",
        "children": [
            {
                "id": "5eece362e3a4f43ca8e9868b",
                "title": "Child 2",
                "children": [
                    {
                        "id": "5eece36ee3a4f43ca8e9868c",
                        "title": "Child 3"
                    }
                ]
            },
            {
                "id": "5eece9210a445e2d0ca78978",
                "title": "Temp"
            }
        ]
    }
]

I need to pull the id from each main object and child object. There can be any number of nested children; it can go down multiple levels so this needs to be able to adapt to however many children an object has. I tried mapping it and pulling the id to a new array but that only gives me the 2 main objects. Is there a way to do this where it creates an array with all id's.

Something like this:

["5eece32be3a4f43ca8e98688", "5eece339e3a4f43ca8e98689", "5eece354e3a4f43ca8e9868a", "5eece362e3a4f43ca8e9868b", "5eece36ee3a4f43ca8e9868c", "5eece9210a445e2d0ca78978"]
2
  • That does not answer my question. That will only give me 2 id's, the 2 main parent objects. I need to go into each parent and get the children ids, and then the children id's inside those children and so on. Commented Jun 19, 2020 at 16:53
  • Ah! There's an even better dupetarget for that. :-) Happy coding! (Since you're new here, I'll just say: Duplicate questions are not bad questions. People tend to think they are, but every question that links to canonical answers improves the ways people can find the answers. Well, nearly every question...) Commented Jun 19, 2020 at 16:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.