I have a tree somthing like this
var datas = {
'tree': [
{
'name': 'name1',
'tree': [
{'name': 'name2'},
{'name': 'name3'},
{
'name': 'name4',
'tree': [
{'name': 'name5'},
{'name': 'name6'}
]
},
{'name': 'name7'}
]
},
{
'name': 'name8',
'tree': [
{'name': 'name9'}
]
}
]
}
I want to find all the parents of the specifig id
for example in the tree demo, if I look for 'name5' I want to find "name1,name4,name5"
I wrote this code but the results wrong and I got the ids of other elements and not the parents only
This is my code
keys: string[];
pathFound: boolean = false;
getLevel(event: iEventBase, id: string, path: string[]): void {
if (this.pathFound) return;
event.content.forEach((key) => {
if (key.id == id) {
if(!path){
path = [];;
}
path.push(key.id);
this.keys = path;
this.pathFound = true;
return;
}
if (key.type === "page") {
if(!path){
path = [];
}
path.push(key.id);
this.getLevel(key, id, path);
}
})
}
}
this.getLevel(state.mainEvent.content.page, event.id, null);