Here is my sample object, suppose If I give the child id "services/bsf/diamSetting" it should return a string like so "nf-bsf -> bsfConfig -> services/bsf/diamSetting"
let arr = [{
"attr": {
"id": "nf-bsf",
"name": "BSF",
"sequence": 1
},
"children": [{
"attr": {
"id": "bsfGeneralConfig",
"name": "General Configurations",
"sequence": 10
},
"children": [{
"attr": {
"id": "services/bsf/bsfGlobalCfg",
"name": "General Settings",
"topic": "bsf.global.cfg",
"sequence": 10
}
}, {
"attr": {
"id": "configurations/bsf/sbiErrorCodes",
"name": "SBI Error Codes",
"topic": "bsf.sbi.errorcodes",
"sequence": 20
}
}]
}, {
"attr": {
"id": "services/bsf/conLoggingLevel",
"name": "Logging Level",
"topic": "consistent.logging.cfg.topics",
"sequence": 20
}
}, {
"attr": {
"id": "bsfServices",
"name": "Service Configurations",
"sequence": 30
},
"children": [{
"attr": {
"id": "services/bsf/managementService",
"name": "Management Service",
"topic": "bsf.managementservice",
"sequence": 10
}
}]
}, {
"attr": {
"id": "bsfConfig",
"name": "Diameter Configurations",
"sequence": 40
},
"children": [{
"attr": {
"id": "services/bsf/diamSetting",
"name": "Settings",
"topic": "common.diamsetting",
"sequence": 10
}
}, {
"attr": {
"id": "configurations/bsf/diamPeerNode",
"name": "Peer Nodes",
"topic": "common.public.diampeernode",
"sequence": 20
}
}, {
"attr": {
"id": "services/bsf/diamRoutingTable",
"name": "Routing Table",
"topic": "common.public.diamroutingtable",
"sequence": 30
}
}, {
"attr": {
"id": "configurations/bsf/diamLoadShedding",
"name": "Load Shedding Profiles",
"topic": "common.public.diamloadshedding",
"sequence": 40
}
}, {
"attr": {
"id": "configurations/bsf/diamMessagePriority",
"name": "Message Priority Profiles",
"topic": "common.public.diammessagepriority",
"sequence": 50
}
}]
}, {
"attr": {
"id": "bsf-sessionViewer",
"name": "Session Viewer",
"sequence": 50
}
}, {
"attr": {
"id": "administration",
"name": "Administration",
"sequence": 60
},
"children": [{
"attr": {
"id": "bsfBulkExportImport",
"name": "Export & Import",
"sequence": 10
}
}]
}]
}];
I have tried the following to traverse, i either get undefined or some wrong path. i don't even get child node with this, and this will be traversed in a reverse order.
var parent = [];
const findParent = (arr, id) => {
for (let i=0; i<arr.length; i++) {
if(arr[i].attr.id == id) {
return parent;
} else if (arr[i].children && arr[i].children.length) {
parent.push(arr[i].attr.id);
findParent(arr[i].children, id)
}
}
};
let x = findParent(arr, "services/bsf/diamSetting");
console.log(x)