title: javascript push into nested tree object
body: <p>I have tried to use a code snippet from this SO question:
  <a href="https://stackoverflow.com/questions/12899609/how-to-add-an-object-to-a-nested-javascript-object-using-a-parent-id">How to add an object to a nested javascript object using a parent id</a></p>
  
  <p>as you try to insert further down the tree it does nit work as undefined is returned.</p>
  
  <p>I've tried to solve this but I''m missing something
  so i need some help please.</p>
  
  <p><a href="http://jsfiddle.net/murray_3/EbJ85/4/" rel="nofollow noreferrer">http://jsfiddle.net/murray_3/EbJ85/4/</a></p>
  
  <p>heres the code:</p>
  
  <pre><code>var ob = {
      name: "root",
      id: 1,
      children: [
          {
              name: "child one",
              id: 11,
              children: [
              {
                  name: "grand child 1",
                  id: 111,
                  children: []
              },
              {
                  name: "grand child 2",
                  id: 112,
                  children: []
              }
          ]},
          {
              name: "child two",
              id: 12,
              children: []
          }
      ]
  };
  
  function findObjectById(root, id) {
      if (root.children) {
          for (var k in root.children) {
              if (root.children[k].id == id) {
                  return root.children[k];
          }
          else if (root.children.length) {
              return findObjectById(root.children[k], id);
              }
          }
      }
   };
  
  
  
  function findObjectByLabel(obj, id) {
     for (i in obj.children) {
        if(obj.children[i].id === id) { 
            document.write( obj.children[i].id);
            l= obj.children[i].children.length +1
            return obj.children[i];
        }
        else { 
              findObjectByLabel( obj.children[i], id);
              }
          }
  };
  
  
  
  
  function traverse(o,spc) {
      for (i in o) {
          if(i == "name"|| i == "id") {
             document.write(spc + i + " : "+o[i]+"&lt;br&gt;");
          }        
          if (typeof(o[i])=="object") {
              //going on step down in the object tree!!
              ss = spc+"_"
              traverse(o[i],ss);
          }
      }
  }
  
  
  var s =""
  traverse(ob,s)
  var boo = findObjectByLabel(ob,111)
  var l = boo.children.length
  document.write(typeof(boo))
  document.write("&lt;br&gt;"+"boo kids l= " +l+"&lt;br&gt;")
  //var boo = findObjectByLabel(ob,111)
  boo.children.push({
      name: "child x",
      id: 11111,
      children: []
  });
  var ll = boo.children.length
  document.write("&lt;br&gt;"+"boo kids l= " +ll+"&lt;br&gt;")
  var s =""
  traverse(ob,s)
  </code></pre>