gpt4 book ai didi

javascript - 递归地遍历一个对象以找到 child ,沿途接 parent ,祖 parent 等

转载 作者:行者123 更新时间:2023-11-30 08:30:35 24 4
gpt4 key购买 nike

我有一个包含菜单的对象。

我想输入一个类别ID并获取类别名称,然后向后移动找到它的parents。这在一个对象中并不容易,所以我正在考虑沿途捕获 parents

我遇到的问题是如何在未找到最终子节点且无处可去时重置 父节点

这就是我正在尝试的:

var data = [
{
"tree_id": "10",
"name": "babies & children",
"parent": null,
"position": "1"
}, {
"tree_id": "2",
"name": "clothing",
"parent": null,
"position": "1",
"children": [{
"tree_id": "15",
"name": "kids",
"parent": "2",
"position": "3",
"children": [{
"tree_id": "78",
"name": "fourToTen",
"parent": "15",
"position": "3",
"children": [{
"tree_id": "102",
"name": "fourToSix",
"parent": "78",
"position": "3"
}]
}]
}]
}, {
"tree_id": "55",
"name": "toys",
"parent": null,
"position": "1",
"children": [{
"tree_id": "35",
"name": "lego",
"parent": "55",
"position": "3"
}]
}
];

var crumbs = [];
function getParts(data, elem) {
for(var i = 0; i < data.length; i++) {
var obj = data[i];
if(obj.children !== undefined){
/* push parent into crumbs */
crumbs.push(obj.name);
if(obj.children[0].tree_id === elem){
/* if we've found what we're looking, we're done */
crumbs.push(obj.children[0].name);
console.log(crumbs);
} else {
/* reset parents */
crumbs = []; /* <-- this is wrong here */
/* not found, keep recursing */
getParts(obj.children, elem);
}
}
}
}
/* I want this to return
[
"clothing",
"kids",
"fourToTen",
"fourToSix"
]

but it returns
[
"fourToTen",
"fourToSix"
]
*/
getParts(data, '102');

问题是,我如何保存 parents 数组,直到我到达行尾并且找不到 child ,然后再重置它?

Here's a fiddle if that's your preferred playround

最佳答案

假设 category id = tree_idcategory_name = name

您需要像对待树一样对待您的data 对象,然后横切它并沿途跟踪父对象。如果找到某些内容,则转储您需要的信息。

所以 data 基本上是您将要遍历的一组对象。

示例:

"use strict";
var data = [
{
"tree_id": "10",
"name": "babies & children",
"parent": null,
"position": "1"
},
{
"tree_id": "2",
"name": "clothing",
"parent": null,
"position": "1",
"children": [{
"tree_id": "15",
"name": "kids",
"parent": "2",
"position": "3",
"children": [{
"tree_id": "78",
"name": "fourToTen",
"parent": "15",
"position": "3",
"children": [{
"tree_id": "102",
"name": "fourToSix",
"parent": "78",
"position": "3"
}]
}]
}]
},
{
"tree_id": "55",
"name": "toys",
"parent": null,
"position": "1",
"children": [{
"tree_id": "35",
"name": "lego",
"parent": "55",
"position": "3"
}]
}
];

// Solution
function transverse(root, tree, targetId) {
tree.push({
catId : root.tree_id,
catName : root.name
});

/* this if() must come first otherwise fails if you want to stop before end */
if (root.tree_id === targetId) {
console.log("Found id:" + targetId+ ", name=" + root.name);
console.log("Dumping parent info => " + JSON.stringify(tree));
return tree;
}

if (root.hasOwnProperty("children") && root.children instanceof Array)
root.children.forEach(child => {
transverse(child, tree, targetId);
});

}

data.forEach(item => {
transverse(item, [], /*Looking for Id=*/"102");
});

console.log("done");

输出:

Found id:102, name=fourToSix
Dumping parent info =>
[
{"catId":"2","catName":"clothing"},
{"catId":"15","catName":"kids"},
{"catId":"78","catName":"fourToTen"},
{"catId":"102","catName":"fourToSix"}]
]

关于javascript - 递归地遍历一个对象以找到 child ,沿途接 parent ,祖 parent 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38539870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com