title: Pushing item into a multi-dimensional tree like structure in JavaScript
body: <p>I have an array of objects which goes on something like this</p>
  
  <pre><code>var obj = [{
      id: 23,
      name: 'Test1',
      children: [{
           id: 24,
           name: 'Test2,
           children: [..]
      },{
           id: 25,
           name: 'Test2,
           children: [..]
      }],
  },{..}]
  </code></pre>
  
  <p>Each children can have multiple sub children, so basically I am trying to represent a graph like structure, similar to the output of <a href="https://github.com/tautologistics/node-htmlparser" rel="nofollow">htmlparser</a>.</p>
  
  <p>I need a function like this:</p>
  
  <pre><code>function(nodeId, json){}
  </code></pre>
  
  <p>The function will need to find the json object in the tree by using the nodeId and insert the json as a children of that parent object. Thats where I am stuck.</p>
  
  <p>I tried writing a recursive function like this to search the appropriate node, however, the trouble arises when I have to insert the json into the actual obj array.</p>
  
  <pre><code>function findNode(nodeId, json, obj){
      if(obj.id == nodeId){
          obj.children.push(json);
      }
      else{
          for(var i=0; i&lt;obj.children.length; i++){
              findNode(nodeId, json, obj.children[i]);
          }
      }
  }
  </code></pre>
  
  <p>It seems the json gets inserted to the obj local to the recursive function, not the actual root obj. How can I can insert it to the parent obj?</p>