2015-12-14 121 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>');