如何遍历JavaScript对象以查找父对象

时间:2015-12-14 21:35:38

标签: javascript angularjs

有没有人知道迭代复杂的javascript对象以找到父节点的有效方法?我有一个返回的对象,我绑定到一个ivhTreeview。我可以让对象绑定但是当我点击子项时我需要派生父节点和祖父节点:

root Item/grandparent (Incident)
   - parent (IncidentStartDate)
       -child (2008)
       -child (2009)
       - and so on

我正在使用的对象示例如下所示

[
    {
        "label": "Document Type",
        "value": "_oCommon.DocumentType",
        "children": [
            {
                "label": "Incident(4891)",
                "value": "Incident",
                "$$hashKey": "object:84",
                "__ivhTreeviewExpanded": true,
                "selected": true,
                "__ivhTreeviewIndeterminate": false,
                "children": [
                    {
                        "label": "Incident Date",
                        "value": "DateIncidentStart",
                        "children": [
                            {
                                "$$hashKey": "object:364",
                                "label": "2008(810)",
                                "value": "01/01/2008"
                            },
                            {
                                "$$hashKey": "object:365",
                                "label": "2009(810)",
                                "value": "01/01/2009"
                            },
                            {
                                "$$hashKey": "object:366",
                                "label": "2010(864)",
                                "value": "01/01/2010"
                            },
                            {
                                "$$hashKey": "object:367",
                                "label": "2011(780)",
                                "value": "01/01/2011"
                            },
                            {
                                "$$hashKey": "object:368",
                                "label": "2012(826)",
                                "value": "01/01/2012"
                            },
                            {
                                "$$hashKey": "object:369",
                                "label": "2013(801)",
                                "value": "01/01/2013"
                            }
                        ]
                    }
                ]
            }
        ],
        "$$hashKey": "object:70",
        "__ivhTreeviewExpanded": true,
        "selected": true,
        "__ivhTreeviewIndeterminate": false
    }
]

我在这里想要完成的是递归抓取树,这样如果我点击2008,我可以看到父DateIncidentStartDocumentType: Incident

的子元素

我采取的方法是两个for循环,第一个迭代我的角度控制器中的大多数集合(是的,这应该在服务中进一步回来,但我只是想让这个工作立即开始)

function getAggregateId(selectedNode, parentTree) {
                vm.lastSelectedNode = selectedNode.value;
                vm.lastSelectedNodeId = selectedNode.objectId;
                vm.selectedNodeParent = parentTree;
                //itterate the tree
                for (var p = 0, tree = parentTree.length; p < tree; p++) {
                    //search each child object for the matching key
                    searchTheChildNode(p, parentTree, selectedNode);
                }
            }

对于参数,ivhTreeview将返回所选节点以及从中选择该节点的树,因此在下面的示例中我有两个

节点:

{
"$$hashKey": "object:364",
"label": "2008(810)",
"value": "01/01/2008"
}

和带有子对象的树:

[{
   "label": "Incident Date",
    "value": "DateIncidentStart",
    [0] Object
    [1] Object
    [2] Object
    [3] Object
    [4] Object
    [5] Object
    [6] Object
...}]

函数searchTheChildNode执行嵌套循环

function searchTheChildNode(index, parent, node) {
    for (var c = 0, child = parent[index].children.length; c < child; c++) {
       for (var nc = 0, items = parent[index].children[c]; nc < items; nc++) {
           if (parent[index].children[c].$$hashKey == node.$$hashKey) {
               console.log('found the parent ' + parent[index].children[c].value);
       }
        }
    }

};

我遇到的情况是我可以看到循环正在运行但是当$$ hasKey的条件设置为true时,日志甚至永远不会发生它只是滚动。我觉得语法错误但我能看到它。

有没有人有任何建议或者在搜索像这样的集合时是否有更好的方法来查找父项和祖父项?

感谢您的任何建议

1 个答案:

答案 0 :(得分:1)

从树的开头迭代并迭代所有孩子。返回的数组包含元素的索引和给定键的值。

function findPath(p, o) {
    function f(o, a) {
        if (Array.isArray(o)) {
            return o.some(function (b, i) {
                if (b[p.key] === p.value) {
                    array = a.concat([i, b[p.key]]);
                    return true;
                }
                return f(b.children, a.concat([i, b[p.key]]));
            });
        }
    }
    var array;
    f(o, []);
    return array;
}

var obj = [{ "label": "Document Type", "value": "_oCommon.DocumentType", "children": [{ "label": "Incident(4891)", "value": "Incident", "$$hashKey": "object:84", "__ivhTreeviewExpanded": true, "selected": true, "__ivhTreeviewIndeterminate": false, "children": [{ "label": "Incident Date", "value": "DateIncidentStart", "children": [{ "$$hashKey": "object:364", "label": "2008(810)", "value": "01/01/2008" }, { "$$hashKey": "object:365", "label": "2009(810)", "value": "01/01/2009" }, { "$$hashKey": "object:366", "label": "2010(864)", "value": "01/01/2010" }, { "$$hashKey": "object:367", "label": "2011(780)", "value": "01/01/2011" }, { "$$hashKey": "object:368", "label": "2012(826)", "value": "01/01/2012" }, { "$$hashKey": "object:369", "label": "2013(801)", "value": "01/01/2013" }] }] }], "$$hashKey": "object:70", "__ivhTreeviewExpanded": true, "selected": true, "__ivhTreeviewIndeterminate": false }];

document.write('<pre>' + JSON.stringify(findPath({ key: 'label', value: '2008(810)' }, obj), 0, 4) + '</pre>');        
document.write('<pre>' + JSON.stringify(findPath({ key: '$$hashKey', value: 'object:368' }, obj), 0, 4) + '</pre>');