Kap*_*era 2 javascript tree recursion
找到我创建的以下父子树对象。我需要找到给定子 ID 的根父级。例如,子 ID - 242 根父 ID 为 238。也有人提出了类似的问题,这是我发现与我的问题非常相似的问题。
我对原始代码做了一些更改,但不适用于子元素。问题就在这里。当它到来时,带有 for 循环的递归函数rootNode.children将不会执行,因为它不循环子级。但是,如果我将 for 循环更改 for (var i = 0; i < rootNode.length; i++) 为,for (var i = 0; i < rootNode.children.length; i++)那么它会在第一个循环上中断,因为没有子循环。我确信通过小的代码更改就可以工作。
var getParent = function (rootNode, rootId) {
if (rootNode.id === rootId)
return rootNode;
//for (var i = 0; i < rootNode.children.length; i++) -- original code line not working first time
for (var i = 0; i < rootNode.length; i++) {
var child = rootNode[i];
if (child.id === rootId)
return child;
if (typeof child.children !== 'undefined')
var childResult = getParent(child, rootId);
if (childResult != null) return childResult;
}
return null;
};
var mytree = [
{
"id": 245,
"parent": "0",
"title": "project1",
"children": [
{
"id": 246,
"parent": "245",
"title": "sub task 1"
}
]
},
{
"id": 238,
"parent": "0",
"title": "project2",
"children": [
{
"id": 240,
"parent": "238",
"title": "sub task 2"
},
{
"id": 242,
"parent": "238",
"title": "sub task 3",
"children" : [
{
"id": 241,
"parent": "242",
"title": "sub task 3.1"
}
]
}
]
},
{
"id": 173,
"parent": "0",
"title": "project3"
}
];
console.log(JSON.stringify(getParent(mytree, 238)['title']));
console.log(JSON.stringify(getParent(mytree, 241)));Run Code Online (Sandbox Code Playgroud)
您需要迭代给定的根节点,因为这是一个数组,而不是一个对象。
function getParent(root, id) {
var node;
root.some(function (n) {
if (n.id === id) {
return node = n;
}
if (n.children) {
return node = getParent(n.children, id);
}
});
return node || null;
}
var mytree = [{ id: 245, parent: "0", title: "project1", children: [{ id: 246, parent: "245", title: "sub task 1" }] }, { id: 238, parent: "0", title: "project2", children: [{ id: 240, parent: "238", title: "sub task 2" }, { id: 242, parent: "238", title: "sub task 3", children: [{ id: 241, parent: "242", title: "sub task 3.1" }] }] }, { id: 173, parent: "0", title: "project3" }];
console.log(getParent(mytree, 238));
console.log(getParent(mytree, 241));Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)
更是一次经典的尝试
function getParent(root, id) {
var i, node;
for (var i = 0; i < root.length; i++) {
node = root[i];
if (node.id === id || node.children && (node = getParent(node.children, id))) {
return node;
}
}
return null;
}
var mytree = [{ id: 245, parent: "0", title: "project1", children: [{ id: 246, parent: "245", title: "sub task 1" }] }, { id: 238, parent: "0", title: "project2", children: [{ id: 240, parent: "238", title: "sub task 2" }, { id: 242, parent: "238", title: "sub task 3", children: [{ id: 241, parent: "242", title: "sub task 3.1" }] }] }, { id: 173, parent: "0", title: "project3" }];
console.log(getParent(mytree, 238));
console.log(getParent(mytree, 241));Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)