I have this structure in my JavaScript:
"treeData": [
{
"id": 29,
"name": "Root",
"children": [
{
"id": 30,
"name": "Sub-Root",
"children": [
{
"id": 31,
"name": "Sub-Sub-Root"
}
]
}
]
},
{
"id": 32,
"name": "Root 2"
}
]
I would like to remove, for example, object with ID 30 (so «Sub-Root» and its children «Sub-Sub-Root»).
Currently, I am working with this recursive method:
for (let i = 0; i < this.treeData.length; i++) {
this.findNested(this.treeData[i], this.selected.id); // ID is string like «30»
}
findNested = function (obj, value) {
if (obj.id === value) {
// delete not working
//delete obj;
obj = null; // Setting to null to delete
console.log('Passed');
}
if (obj && obj.children && obj.children.length > 0) {
for (let j = 0; j < obj.children.length; j++) {
this.findNested(obj.children[j], value);
}
}
}
The problem is, even I set obj to null, my treeData object is not affected and I don't get why.
My question is: how can I remove and element (and all its sub-elements) from my treeData knowing only the value of the "id" element ?
treeData