[英]How to get a child element by id by its parent id element using pure Javascript
[英]I want to get all parent nodes along with its child, using a child id for a given data in javascript
这是我的样本 object,假设如果我给子 ID“services/bsf/diamSetting”,它应该返回一个像这样的字符串“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
}
}]
}]
}];
我尝试了以下遍历,我要么得到未定义的路径,要么得到一些错误的路径。 我什至没有得到子节点,这将以相反的顺序遍历。
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)
您的parent
数组破坏了 function 封装,造成了多个问题:
parent
可能会从之前的遍历中获得过时的信息。 这是解决这些问题的方法。 我正在使用一个闭包来将结果数组限制在其 function 的本地范围内, pop
以撤消所有未最终导致目标的push
,以及some
从搜索中退出并尽快展开调用堆栈找到了目标。
const tree = [ { "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 } } ] } ] } ]; const findPath = (children, targetId) => { const path = []; (function search(children) { return children?.some(child => { path.push(child.attr.id); if (child.attr.id === targetId || search(child.children)) { return true; } path.pop(); }); })(children); return path; }; const path = findPath(tree, "services/bsf/diamSetting"); console.log(path.join(" -> "));
使用 function 之外的parent
变量将破坏递归,因为每个递归级别共享一个公共变量,这意味着一个递归的结果可能会覆盖另一个递归的先前结果。
您应该改为使parent
变量成为findParent
本身的私有变量,以便每个递归都将创建自己的数组并修改/返回它。
这是一个修改后的代码,它使用第三个参数hierarchy
,其功能与您的parent
相同,但对每个 function 调用都是私有的( 闭包):
const 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 } } ] } ] } ]; const findParent = ( arr, id, hierarchy = [] // Use an array here to store parents instead of using an external variable. ) => { for (let i = 0; i < arr.length; i++){ // If id is found, push it to the result array, stop the loop, then return the result. if (arr[i].attr.id === id){ hierarchy.push(arr[i].attr.id); return hierarchy; } // If id is not found, proceed to its children if (arr[i].children && arr[i].children.length){ const childResult = findParent( arr[i].children, id, [...hierarchy, arr[i].attr.id] // Pass the found parents so far to its child recursion. ); // Only stop the loop and return the result if id is found in child. if (childResult){ return childResult; } } // Proceed to the next loop if id is not found in current item AND its offspring children. } // Return null if id is not found in this branch. return null; }; let x = findParent(arr, "services/bsf/diamSetting"); console.log(x && x.join(' -> '));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.