i have array:
[
{id: 1, name: 'Parent1'},
{id: 2, name: 'Child1', parent: {id: 1, name: 'Parent1'}},
{id: 3, name: 'Child2', parent: {id: 1, name: 'Parent1'}},
{id: 4, name: 'GrandChild1', parent: {id: 3, name: 'Child2'}},
{id: 5, name: 'GrandChild2', parent: {id: 3, name: 'Child2'}}
]
I need to write a function with an id argument that will return an array that does not include the element with id and all of its children and children of childrens. Help pls
I tries write recursing function but fallen
4
Answers
it should be work!
You could store the unwanted parents for later filtering out.
This approach works for unsorted items.
That is how I would approach the problem