Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 递归地遍历一个对象以找到子对象,一路上接父母、祖父母等_Javascript - Fatal编程技术网

Javascript 递归地遍历一个对象以找到子对象,一路上接父母、祖父母等

Javascript 递归地遍历一个对象以找到子对象,一路上接父母、祖父母等,javascript,Javascript,我有一个由菜单组成的对象 我想输入一个类别ID并获取类别名称,然后向后移动找到它的父项。这在一个对象中并不容易,所以我想在这个过程中捕捉父对象 我遇到的问题是,当找不到最终子项且无处可去时,如何重置父项 这就是我正在尝试的: var数据=[ { “树id”:“10”, “姓名”:“婴儿和儿童”, “父项”:空, “位置”:“1” }, { “树id”:“2”, “姓名”:“服装”, “父项”:空, “职位”:“1”, “儿童”:[{ “树id”:“15”, “姓名”:“孩子们”, “家长”:“

我有一个由菜单组成的对象

我想输入一个
类别ID
并获取
类别名称
,然后向后移动找到它的
父项
。这在一个对象中并不容易,所以我想在这个过程中捕捉
父对象

我遇到的问题是,当找不到最终子项且无处可去时,如何重置父项

这就是我正在尝试的:

var数据=[
{
“树id”:“10”,
“姓名”:“婴儿和儿童”,
“父项”:空,
“位置”:“1”
}, {
“树id”:“2”,
“姓名”:“服装”,
“父项”:空,
“职位”:“1”,
“儿童”:[{
“树id”:“15”,
“姓名”:“孩子们”,
“家长”:“2”,
“职位”:“3”,
“儿童”:[{
“树id”:“78”,
“名称”:“fourToTen”,
“家长”:“15”,
“职位”:“3”,
“儿童”:[{
“树id”:“102”,
“名称”:“fourToSix”,
“家长”:“78”,
“位置”:“3”
}]
}]
}]
}, {
“树id”:“55”,
“名称”:“玩具”,
“父项”:空,
“职位”:“1”,
“儿童”:[{
“树id”:“35”,
“名称”:“乐高”,
“家长”:“55”,
“位置”:“3”
}]
}
];
var-crumbs=[];
函数getParts(数据、元素){
对于(变量i=0;icrumbs=[];/*假设
类别id=tree\u id
类别名称=name

你需要像对待一棵树一样对待你的
数据
对象,然后横穿它,沿途跟踪父对象。如果发现了什么,那么就转储你需要的信息

因此,
数据
基本上是一组您将要遍历的对象

示例:

"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"}]
]
输出:

"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"}]
]

假设
category id=tree\u id
category\u name=name

你需要像对待一棵树一样对待你的
数据
对象,然后横穿它,沿途跟踪父对象。如果发现了什么,那么就转储你需要的信息

因此,
数据
基本上是一组您将要遍历的对象

示例:

"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"}]
]
输出:

"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"}]
]

以下是一种简洁实用的方法:

data=[{“tree\u id”:“10”,“name”:“baby&children”,“parent”:null,“position”:“1”},{“tree\u id”:“2”,“name”:“1”,“children”:[{“tree\u id”:“15”,“name”:“children”,“parent”:“2”,“position”:“3”,“children”:[{“tree\u id”:“78”,“name”:“fourToTen”,“parent”:“15”,“position”:“3”,“children”:[{“tree\u id”:“102”,“name”:“fourToSix”,“父项”:“78”,“位置”:“3”}]}]},{“树id”:“55”,“名称”:“玩具”,“父项”:null,“位置”:“1”,“子项”:[{“树id”:“35”,“名称”:“乐高”,“父项”:“55”,“位置”:“3”}]
// 
第一个=(ary,fn)=>ary.reduce((r,x)=>r | | fn(x),false);
locate=(数据,id)=>u locate({children:data},id,[]);
_locate=(节点、id、路径)=>node.tree\u id==id?路径
:第一个(node.children | |[],n=>_locate(n,id,path.concat(n));
res=locate(数据'102').map(n=>n.name)

console.log(res);
这里有一种简洁的功能方式:

data=[{“tree\u id”:“10”,“name”:“baby&children”,“parent”:null,“position”:“1”},{“tree\u id”:“2”,“name”:“1”,“children”:[{“tree\u id”:“15”,“name”:“children”,“parent”:“2”,“position”:“3”,“children”:“fourToTen”,“parent”:“15”,“position”:“3”,“children”:[{“tree\u id”:“102”,“name”:“fourToSix”父项“:”78“,”位置“:”3“}]}],{”树id“:”55“,”名称“:”玩具“,”父项“:”空,“位置“:”1“,”子项“:[{”树id“:”35“,”名称“:”乐高“,”父项“:”55“,”位置“:”3“}]]
// 
第一个=(ary,fn)=>ary.reduce((r,x)=>r | | fn(x),false);
locate=(数据,id)=>u locate({children:data},id,[]);
_locate=(节点、id、路径)=>node.tree\u id==id?路径
:第一个(node.children | |[],n=>_locate(n,id,path.concat(n));
res=locate(数据'102').map(n=>n.name)

console.log(res);
Globals不好,请尝试重写您的函数,以便将
wantId
作为参数传递。确实,Globals不好。更新后的答案,左acomment@DarrenSweeney我明白你的意思了。谢谢你指出这一点。希望它能帮助你解决你的问题:)全局变量不好,请尝试重写你的函数,以便
wantId
是p作为参数assed。确实全局值不好。更新的答案,左acomment@DarrenSweeney我明白你的意思。谢谢你指出。希望它能帮助你解决你的问题:)我可能会遇到浏览器兼容性问题吗?@DarrenSweeney-看看源代码,你会觉得
Array.reduce
Array很好.concat
Array.map
都是ECMA 5.1语法。@georg的解决方案基于函数式编程概念,仅此而已。我可能会遇到浏览器兼容性问题吗?@DarrenSweeney-查看源代码,您会发现
Array.reduce
Array.concat
Array.map
都是ECMA5.1语法。@georg的解决方案基于函数式编程概念,仅此而已。