迭代地迭代一个物体来找到孩子,一路上捡起父母,祖父母等

时间:2016-07-23 08:41:10

标签: javascript

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

我想输入category ID并获取category name,然后向后移动以找到它parents。在一个物体中这并不容易,所以我想要在此过程中抓住parents

我遇到的问题是reset 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');

这就是我正在尝试的:

&#13;
&#13;
parents
&#13;
&#13;
&#13;

问题是,如何保存has数组,直到我在行尾并且找不到子项,然后重置它?

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

2 个答案:

答案 0 :(得分:3)

假设 protected async override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Ok) { var imageData = GetBytes(ContentResolver.OpenInputStream(data.Data)); } } category id = tree_id

您需要像对待树一样对待category_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");

答案 1 :(得分:1)

这是一种紧凑的功能方式:

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

// 

first = (ary, fn) => ary.reduce((r, x) => r || fn(x), false);

locate = (data, id) => _locate({children: data}, id, []);

_locate = (node, id, path) => node.tree_id === id ? path
    : first(node.children || [], n => _locate(n, id, path.concat(n)));

res = locate(data, '102').map(n => n.name)
console.log(res);