last day (15 days later) » 

7:13 AM
3
A: JavaScript: Find all parents for element in tree recursive

Nina ScholzYou could use a recursive approach by checking the name and return the name in an array or return the path to the object by calling the function again. function find(array, name) { var result; array.some(o => { var temp; if (o.name === name) { return ...

 
This works, but this is not returning the same object reference. I mean in my case I have applied this method and push the values in immediate parent of a node, but this does not reflect in the actual tree??
 
please check the second code block.
 
This (With parent elements) looks the same, can you please what changes, you have made :) ?
 
maybe i don't understand your goal, please add an example of the wanted result.
 
Actually i want to delete a node from the tree, so i find the immediate parent node and filter the data of its child, by using this code block, which works perfectly fine to locate the all parents objects. But the references it returns are not the same from the tree nodes, so even if I filter the child record of immediate parent node, it does not reflect in the tree node!!
 
7:13 AM
do you want to mutate the data? or a new tree upt to the wanted node?
 
anyway will work, ultimate goal is to delete the node
private _findTreeNodeParent(data, nodeId) {
    let result;
    data.some((node) => {
        let nodeInfo;
        if (node.id === nodeId) {
            return result = [node];
        }
        nodeInfo = this._findTreeNodeParent(node.subContentTypes, nodeId);
        if (nodeInfo) {
            return result = [node, ...nodeInfo];
        }
    });
    return result;
}
const _immediateParent = _parentList[_parentList.length - 2];
_immediateParent.subContentTypes =
_immediateParent.subContentTypes.filter((_contentType) => _contentType.id !== contentTypeId);
tree is of type {id,name,subContentTypes}
 
oh hi and thanks
:0
 
the result should contains the wanted nodes to the target object only?
 
yes
the actuall reference
or may b any other way to delete the particular from a tree
 
7:18 AM
one question do you want the same object reference (with out unwanted), or a new one?
 
both will work but without the unwanted node/object
you got my point ?
 
yes, one moment please
 
yeah sure!1 thanks :)
 
7:53 AM
please see edit.
 
let me chk
this will return the parents object reference of actual passed tree?
 
it qwould be easier, if you add the wanted result.
 
8:08 AM
ok
suppose this is the tree
{
id: 1,
name : "A",
subContentTypes : [
{
id: 2,
name: "B",
subContentTypes: []
},
{
id: 3,
name: "C",
subContentTypes: [
{
id: 4,
name: "D",
subContentTypes: []
},
{
id: 5,
name: "E",
subContentTypes: [
{
id: 6,
name: "F",
subContentTypes: []
}
]
}
]
}
]
}
i want to delete the node with id 6
so it will become
{
id: 1,
name : "A",
subContentTypes : [
{
id: 2,
name: "B",
subContentTypes: []
},
{
id: 3,
name: "C",
subContentTypes: [
{
id: 4,
name: "D",
subContentTypes: []
},
{
id: 5,
name: "E",
subContentTypes: [

]
}
]
}
]
}
tree without node id 6
 
8:23 AM
you are still aware, that finding all nodes to a given name is different than to remove a specified node?
 
8:36 AM
maybe your example is misleading ...
please see edit.
 
9:24 AM
ok thanks :)
 
does it work?
 

last day (15 days later) »