title: Javascript: Find all parents for element in tree
body: <p>I have the objects tree, and i can't found all parents for concrete object id. Imagine i need to add some new field to each parent for object with id = 5. Can someone help please with recursive loop through tree</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var tree = {
    id: 1,
    children: [
     {
    id: 3,
    parentId: 1,
    children: [
       {
      id: 5,
      parentId: 3,
      children: []
     }
    ]
   }
    ]
  }
  
  console.log(searchTree (tree, 5));
  
  function searchTree (tree, nodeId){
        for (let i = 0; i &lt; tree.length; i++){
          if (tree[i].id == nodeId) {
              // it's parent
              console.log(tree[i].id);
              tree[i].newField = true;
              if (tree[i].parentId != null) {
                searchTree(tree, tree[i].parentId);
              }
          }
        }
   }</code></pre>
  </div>
  </div>
  </p>