2015-12-14 95 views
2

有谁知道有效方法来遍历复杂的JavaScript对象以查找父节点吗?我有一个对象被返回,我绑定到一个ivhTreeview。我可以要绑定的对象,但是当我点击一个子项,我需要得到父母和祖父母节点:如何遍历JavaScript对象以查找父对象

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年,我能看到父母为DateIncidentStart这是我采取的方法是两个for循环的DocumentType: Incident

一个子元素中,第一个迭代是我的角度控制器内的outter最集合(是的,这应该是更久远的一个服务,但我只是试图让这个工作现在)

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日志,甚至永远不会发生,它只是在滚动。我觉得有句法错误,但我可以看到它。

有没有人有任何建议或有没有更好的方法来定位父母和祖父母项目时搜索这样的集合?

感谢您的任何建议

+0

这或多或少是我参加这样的各种情况的方法。据我所知,你不会用JSON获得更高效的解决方案。我不太了解这项技术,所以我不能担保,但我已经看到提及LINQ for JavaScript。 https://linqjs.codeplex.com/ – Csteele5

+0

我在互联网上发现,如果您需要从嵌套对象访问js对象的父对象,则可以将父对象的引用保存在子对象中。 – Gonzalo

+0

只需浏览一次对象,并按照@Gonzalo的建议将所有必需的引用添加到子节点。 – scareddragon

回答

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