I have a very large and deep object where I have to search for a node with certain matches and delete it.
For this I have made a recursive function where I am passing the node in which it is located.
In the following example it can be seen that it is not deleted correctly. the ideal would be to pass it by reference to the C style but as far as I know it is not possible in javascript. I don't know if there is any other way to pose the problem or trick to delete the node really
const arbol = {
id: 1,
val: "XXX",
childs: [
{
id: 2,
val: "XXX",
childs: [
{
id: 3,
val: "DEBERIA BORRARSE",
childs: [
{
id: 33,
val: "XXX",
childs: []
},
{
id: 44,
val: "XXX",
childs: []
}
]
},
{
id: 4,
val: "XXX",
childs: []
},
{
id: 5,
val: "XXX",
childs: [
{
id: 6,
val: "XXX",
childs: []
},
{
id: 7,
val: "DEBERIA BORRARSE",
childs: []
}
]
}
]
}
]
}
const listaEliminar = [3,7];
function eliminarNodo(nodo) {
if (listaEliminar.includes(nodo.id)){
console.log("borra", nodo)
delete nodo;
}else {
nodo.childs.forEach(n => {
eliminarNodo(n)
})
}
}
console.log("ANTES:",arbol)
eliminarNodo(arbol)
console.log("DESPUES",arbol)