Let’s say I have an array of nested objects such as:
const data = [ { id: 1, children:[ { id: 2, children:[ { id: 3, children:[ { id: 4, children: [], }, { id: 5, children: [], } ], }, ], }, ], }, { id: 8, children:[ { id: 9, children:[ { id: 10, children:[], }, ], }, ], }, ];
And I would like to return a new array with id: 3 excluded for example. So it would become:
const data = [ { id: 1, children:[ { id: 2, children:[], }, ], }, { id: 8, children:[ { id: 9, children:[ { id: 10, children:[], }, ], }, ], }, ];
The excluded id can be from any level, and it and its children should be removed. What’s the best approach to do this? I’ve tried several other solutions with no luck so far.
Advertisement
Answer
You can use recursion and Array.reduce
,..
eg.
const data=[{id:1,children:[{id:2,children:[{id:3,children:[{id:4,children:[]},{id:5,children:[]}]},]},]},{id:8,children:[{id:9,children:[{id:10,children:[]},]},]},] function removeId(data, id) { return data.reduce((a,v) => { if (v.id !== id) a.push({...v, children: removeId(v.children, id)}); return a; }, []); } const r = removeId(data, 3); console.log(r);