I have parent-child JSON data and I want all the child (nested children) from the selected parent.
For example, I have JSON data:
[{ "id": 1, "parent": 0, "name": "Parent" }, { "id": 2, "parent": 1, "name": "Child 1" }, { "id": 3, "parent": 2, "name": "Grand Child 1" }, { "id": 4, "parent": 2, "name": "Grand Child 2" }, { "id": 5, "parent": 1, "name": "Child 2" }]
And I have a findAllChildren (1) function, where "1" is the "parent", and then the result of the function should be:
[{ "id": 2, "parent": 1, "name": "Child 1" }, { "id": 3, "parent": 2, "name": "Grand Child 1" }, { "id": 4, "parent": 2, "name": "Grand Child 2" }, { "id": 5, "parent": 1, "name": "Child 2" }]
And in another case, if I call findAllChildren (2), the result of the function should look like this:
[{ "id": 3, "parent": 2, "name": "Grand Child 1" }, { "id": 4, "parent": 2, "name": "Grand Child 2" }]
What is the correct way to create a function to solve this case? Thanks.