I have the objects tree, and i can\'t found all parents for concrete object id. Imagine i need to add some new field to each parent for object with id = 5. Can someone help
Here is an example of a working recursive function.
Play around with it for a while and you should be golden
var tree = {
id: 1,
children: [{
id: 3,
parentId: 1,
children: [{
id: 5,
parentId: 3,
children: []
}]
}]
}
function mapit(node, parent = null) {
node.parent = parent;
if (node.children.length > 0) {
for (var i = 0; i < node.children.length; i++) {
var child = node.children[i];
mapit(child, node);
}
}
}
mapit(tree);
console.log(tree);