javascript - 查找 javascript 树对象数组的父级

标签 javascript recursion tree

找到我创建的下面的父子树对象。我需要找到给定子 ID 的根父级。例如 child id - 242 root parent id 是 238。类似的问题已经被问到,这是我发现与我的问题非常相似的问题。

Convert parent-child array to tree

我稍微更改了原始代码但不适用于子元素。问题就在这里。递归函数 rootNode.children for 循环将不会执行,因为它不会遍历子项。但是如果我改变 for 循环 for (var i = 0; i < rootNode.length; i++)for (var i = 0; i < rootNode.children.length; i++)然后它在第一个循环中中断,因为没有 child 。我敢肯定,只需更改少量代码,这就能奏效。

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)));

最佳答案

您需要迭代给定的根节点,因为这是一个数组,而不是一个对象。

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));
.as-console-wrapper { max-height: 100% !important; top: 0; }

更经典的尝试

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));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 查找 javascript 树对象数组的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49401319/

相关文章:

javascript - 如何使用下拉选择填充文本字段

javascript - Paypal 结帐错误 - ReferenceError : actions is not defined

c - 数独 9x9 C 盒

java - 我如何向我的 gwt 树提供数据

c - 为什么我的霍夫曼代码的节点没有正确排序? C

c++ - 状态机实现

javascript - 使段落 Prop 中的某些文本在 react 中加粗的最佳方法是什么?

R 中的递归编程

c - 如何在 C 中递归地查找另一个字符串中的字符串位置?

javascript - 革命性的 slider 快速淡入淡出