查找JavaScript树对象数组的父级 [英] Find the parent of a javascript tree object array

查看:64
本文介绍了查找JavaScript树对象数组的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到我创建的波纹管父子树对象.我需要找到给定子代ID的根父代.例如,孩子ID-242根父母ID是238.有人问过类似的问题,而这是我发现与我的问题非常相似的一个问题.

Find the bellow parent child tree object I have created. I need to find the root parent of a given child id. For example child id - 242 root parent id is 238. There are similar questions have been asked and this is the one I found very similar to my question.

将父子数组转换为树

我稍微更改了原始代码,但不适用于子元素.问题在这里.带有 rootNode.children for循环的递归函数将不会执行,因为它不会遍历子级.但是如果我将for循环 for(var i = 0; i< rootNode.length; i ++)更改为 for(var i = 0; i< rootNode.children.length;i ++),由于没有子项,因此它在第一个循环上中断.我敢肯定,只需更改少量代码就可以使它工作.

I have change the original code a bit But not working for child element. The issue is here. when it comes the recursive function with rootNode.children for loop will not execute since it does not loop through children. But if I change the for loop for (var i = 0; i < rootNode.length; i++) to for (var i = 0; i < rootNode.children.length; i++) then it break on first loop since does not have children. I'm sure with small code change this can make work.

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

推荐答案

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

You need to iterate the given root node, because this is an array, not an object.

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树对象数组的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆