title: Typescript - Why does this recursive function skips some numbers?
body: <p>From the server, I receive this JSON object. It represents an organigram of a company and the associated departments. </p>
  
  <p>I need to be able to choose a company, and with the ID of the company, I need to pass to an array of numbers the ID's of the associated departments.</p>
  
  <p>For that, I've created this recursive function. It works, but, skips 3 departments, which are placed within another department</p>
  
  <p>This is the JSON file</p>
  
  <pre><code>{
    "cd": 1,
    "cd_base": 0,
    "nome": "EMPRESA A",
    "children": [
      {
        "cd": 2,
        "cd_base": 1,
        "nome": "Departamento A",
        "children": [
          {
            "cd": 4,
            "cd_base": 2,
            "nome": "Serviço A1",
            "children": []
          },
          {
            "cd": 15,
            "cd_base": 2,
            "nome": "Serviço A2",
            "children": []
          }
        ]
      },
      {
        "cd": 3,
        "cd_base": 1,
        "nome": "Departamento B",
        "children": [
          {
            "cd": 7,
            "cd_base": 3,
            "nome": "Serviço B1",
            "children": []
          }
        ]
      },
      {
        "cd": 186,
        "cd_base": 1,
        "nome": "Departamento XX",
        "children": []
      }
    ]
  }
  </code></pre>
  
  <p>And this is the function in Typescript</p>
  
  <pre><code>recursiveFunction(res: any): any[] {
      const numbers = new Array(); // to store the ID
      console.log('Im on ' + res.cd + ' | ' + res.nome);
      numbers.push(res.cd);
      if (res.children.length &gt; 0) {
        console.log(res.cd + ' | ' + res.nome + ' has children');
        res.children.forEach((row) =&gt; {
          numbers.push(row.cd);
          this.recursiveFunction(row);
        });
      } else {
        console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children');
      }
      return numbers;
    }
  </code></pre>
  
  <p>And this is the return of that function to the console</p>
  
  <pre><code>Im on 1 | EMPRESA A 
  1 | EMPRESA A has c
  Im on 2 | Departamento A 
  2 | Departamento A has children 
  Im on 4 | Serviço A1 
  4 | Serviço A1 doesn't have any children 
  Im on 15 | Serviço A2 
  15 | Serviço A2 doesn't have any children 
  Im on 3 | Departamento B 
  3 | Departamento B has children 
  Im on 7 | Serviço B1 
  7 | Serviço B1 doesn't have any children 
  Im on 186 | Departamento XX 
  186 | Departamento XX doesn't have any children 
  </code></pre>
  
  <p>Then I log the <em>numbers</em> array and the result is <em>1,2,3,186</em></p>
  
  <pre><code> this.numbers.forEach(row =&gt; {
      console.log(row);
    });
   // 1, 2, 3, 186
  </code></pre>
  
  <p>It adds the CD 1, 2, 3 and 186, but skips the 4, 7 and 15.
  All of those are a branch/node <strong>within</strong> another branch/node</p>
  
  <p>What am I missing? Is recursive the best way to do this? Is there a simpler way?</p>
  
  <p>Any help is appreciated</p>