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

如何遍历JavaScript对象以查找父对象,javascript,angularjs,Javascript,Angularjs,有人知道一种有效的方法来迭代复杂的javascript对象来定位父节点吗?我有一个被返回的对象,我正在绑定到一个an。我可以获取要绑定的对象,但当我单击子项时,我需要派生父节点和祖父母节点: root Item/grandparent (Incident) - parent (IncidentStartDate) -child (2008) -child (2009) - and so on 我正在处理的对象的示例如下所示 [ {

有人知道一种有效的方法来迭代复杂的javascript对象来定位父节点吗?我有一个被返回的对象,我正在绑定到一个an。我可以获取要绑定的对象,但当我单击子项时,我需要派生父节点和祖父母节点:

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,我就可以看到父元素was
DateIncidentStart
,它是
DocumentType:Incident

我采用的方法是两个for循环,第一个迭代angular controller中最外层的集合(是的,这应该是服务中的最外层集合,但我现在正试图完成这项工作)

以及包含子对象的树:

[{
   "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);
       }
        }
    }

};
函数搜索子节点(索引、父节点、节点){
对于(var c=0,child=parent[index].children.length;c
我被卡住的地方是我可以看到循环在运行,但是当$$hasKey的条件设置为true时,日志甚至从未出现,它只是继续运行。我觉得有些语法错误,但我能看出来

在搜索这样的收藏时,有没有人有什么建议,或者有没有更好的方法来定位父项和祖父母项


感谢您的建议

只需从树的开头开始迭代,然后迭代所有子项即可。返回的数组包含元素的索引和给定键的值

函数findPath(p,o){
函数f(o,a){
if(数组isArray(o)){
返回o.some(函数(b,i){
如果(b[p.key]==p.value){
数组=a.concat([i,b[p.key]]);
返回true;
}
返回f(b.children,a.concat([i,b[p.key]]);
});
}
}
变量数组;
f(o,[]);
返回数组;
}
var obj=[{“label”:“文档类型”,“值”:“{oCommon.DocumentType”,“子项”:[{“label”:“事件(4891)”,“值”:“事件”;“$$hashKey”:“对象:84”,“{ivhTreeviewExpanded”:true,“选定的”:true,“ivhtreeviewUndeterminate”:false,“子项”:[{“label”:“事件日期”,“值”:“DateIncidentStart”,“子项”:[{“$$hashKey”:“对象:364”,“标签”:“2008(810)”,“值”:“01/01/2008”},{“$$hashKey”:“对象:365”,“标签”:“2009(810)”,“值”:“01/01/2009”},{“$$hashKey”:“对象:366”,“标签”:“2010(864)”,“值”:“01/01/2010”},{“$$hashKey”:“对象:367”,“标签”:“2011(780)”,“值”:“01/01/2011”,{“$$hashKey”:“对象:368”,“标签”:“2012(826)”,“值”:”2012年1月1日“},{$$hashKey:”对象:369“,”标签“:”2013(801)”,”值“:”01/01/2013“}]}],“$$hashKey:”对象:70“,”ivhTreeviewExpanded“:true,“selected”:true,“IvhtreeViewUndeterminate”:false}];
write(“”+JSON.stringify(findPath({key:'label',value:'2008(810)},obj),0,4)+’);

write(“”+JSON.stringify(findPath({key:'$$hashKey',value:'object:368'},obj),0,4)+“”)
这或多或少是我在每种情况下采取的方法。据我所知,使用JSON不会得到更有效的解决方案。我对这项技术了解不多,所以我不能保证这一点,但我看到有人提到LINQ for JavaScript。如果您需要访问父级,我在互联网上找到了什么对于嵌套对象中的js对象,您可以将父对象的引用保存在子对象中。只需遍历该对象一次,并按照@Gonzalo的建议将所有需要的引用添加到子节点。您是否有lodash或下划线?如果您使用嵌套ng repeat,则可以绑定一个函数,单击即可获取父对象。共享您的html,我将我来演示一下。
[{
   "label": "Incident Date",
    "value": "DateIncidentStart",
    [0] Object
    [1] Object
    [2] Object
    [3] Object
    [4] Object
    [5] Object
    [6] Object
...}]
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);
       }
        }
    }

};