Anyone know how to recursively find and delete an object from an array structured like this with infinite nested objects with the same structure?
My bad attempt at this:
Is there a way to find the object by id and then delete that specific object only? Thanks!
JavaScript:
[
{
id: 1,
nest: [
{
id: 2,
nest: [{id: 3, nest: [{..continues..}]}],
},
{
id: 4,
nest: [{id: 5, nest: [{..continues..}]}],
},
],
},
{...continues..}
];
My bad attempt at this:
JavaScript:
const handleDelete = (id: number) => {
if (objToFindAndDelete) {
const tempObj = JSON.parse(JSON.stringify(objToFindAndDelete)) as TObjStructure;
const findAndDelete = (menu: TObjStructure, id: number) => {
menu.map((item, index) => {
if (item.menu_id === id) {
const index = tempObj.indexOf(item);
tempObj.splice(index, 1);
} else if (item.nest && item.nest.length > 0) {
findAndDelete(item.nest, id);
}
});
};
findAndDelete(tempObj, id);
}
};
Is there a way to find the object by id and then delete that specific object only? Thanks!