title: For loop is not working inside recursive function
body: <p>1.I have a use case where i want the parent to be made false if any last child &quot;isSelected&quot; is true. For example the id 4 isSelcted is true so the parents of 4 are 3,2,1 whose key &quot;collapse&quot; should be made false.</p>
  <p>2.Example 2: If Id 7 isSelected is true then its parent 6,2,1 whose key &quot;collapse&quot; should be made false.</p>
  <p>3.I have a tree in angular , on edit page I'll have a data in the above format. I have to expand the tree if any one of the child is selected. For example id 4 is the last child which contains isSelected falg and is true. Hence I have to make all the parent &quot;collapse&quot; false so that using ngbBootstrap [collapse] property i'll expand the tree.</p>
  <p>4.The for loop is not working, the first child of every parent is executed but the loop for children is not working, may I know what am I missing?</p>
  <p>5.This code is not working as am missing something, please correct where am going wrong.</p>
  <pre><code>let object = {
      children: [{
          ID: 1,
          collapse: false,
          children: [{
              ID: 2,
              collapse: true,
              children: [{
                  ID: 3,
                  collapse: true,
                  children: [{
                      ID: 4,
                      isSelected: true
                  }, {
                      ID: 5,
                      isSelected: true
                  }]
              }, {
                  ID: 6,
                  collapse: false,
                  children: [{
                      ID: 7,
                      isSelected: false
                  }, {
                      ID: 8,
                      isSelected: false
                  }]
              }]
          }, {
              ID: 9,
              collapse: true,
              children: [{
                  ID: 10,
                  isSelected: false
              }, {
                  ID: 11,
                  isSelected: true
              }]
          }]
      }]
  };
  
  function recursiveUpdate(object) {
  
      function rec(obj){
          if(!obj.children){
              if(obj.isSelected){return false}else{return true}
          }else{
              for(let i=0;i&lt;obj.children.length;i++){
                  let res = rec(obj.children[i]);
                  obj.collapse = res;
                  return res;
              }
          }
      }
      rec(object);
  }
  
  recursiveUpdate(object);
  
  console.log(object);
  
  
  </code></pre>