title: How to iterate through a JavaScript object to find the parent object
body: <p>Does anyone know of an efficient way to iterate over a complex javascript object to locate the parent node? I have an object being returned that I am binding to an an <a href="https://github.com/iVantage/angular-ivh-treeview?files=1" rel="nofollow">ivhTreeview</a>. I can get the object to bind but when I click on an child item I need to derive the parent and grandparent node:</p>
  
  <pre><code>root Item/grandparent (Incident)
     - parent (IncidentStartDate)
         -child (2008)
         -child (2009)
         - and so on
  </code></pre>
  
  <p>A sample of the object that I am working with looks like this</p>
  
  <pre><code>[
      {
          "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
      }
  ]
  </code></pre>
  
  <p>What I am trying to accomplish here is to recursively crawl the tree so that if I click on 2008 I can see the parent was <code>DateIncidentStart</code> which is a child element of <code>DocumentType: Incident</code></p>
  
  <p>The approach I took was two for loops, the first one iterates the outter most collection within my angular controller (yes this should be further back in a service but I am just trying to get this work right now)</p>
  
  <pre><code>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 &lt; tree; p++) {
                      //search each child object for the matching key
                      searchTheChildNode(p, parentTree, selectedNode);
                  }
              }
  </code></pre>
  
  <p>for the parameters the ivhTreeview will return the selected node and the tree from which that node was selected so in this example below I have both </p>
  
  <p>the node:</p>
  
  <pre><code>{
  "$$hashKey": "object:364",
  "label": "2008(810)",
  "value": "01/01/2008"
  }
  </code></pre>
  
  <p>and the tree with children objects:</p>
  
  <pre><code>[{
     "label": "Incident Date",
      "value": "DateIncidentStart",
      [0] Object
      [1] Object
      [2] Object
      [3] Object
      [4] Object
      [5] Object
      [6] Object
  ...}]
  </code></pre>
  
  <p>The function searchTheChildNode does the nested loop</p>
  
  <pre><code>function searchTheChildNode(index, parent, node) {
      for (var c = 0, child = parent[index].children.length; c &lt; child; c++) {
         for (var nc = 0, items = parent[index].children[c]; nc &lt; items; nc++) {
             if (parent[index].children[c].$$hashKey == node.$$hashKey) {
                 console.log('found the parent ' + parent[index].children[c].value);
         }
          }
      }
  
  };
  </code></pre>
  
  <p>Where I am stuck is I can see the loops running but when the condition of $$hasKey is set to true the log even never occurs it just rolls on. I feel there is something syntactically wrong but I can see it. </p>
  
  <p>Does anyone have any suggestions OR is there a better approach to locating the parent and grandparent items when searching a collection like this?</p>
  
  <p>Thanks for any suggestions</p>