title: How to use the result of an iteration to re-iterate?
body: <p>I need to create a new array from another with the condition:
  for example from an array</p>
  <pre><code>mainArr: [
          {
              &quot;id&quot;:1,
              &quot;name&quot;:&quot;root&quot;
          },
          {
              &quot;id&quot;:2,
              &quot;parentId&quot;:1,
              &quot;name&quot;:&quot;2&quot;
          },
          {
              &quot;id&quot;:148,
              &quot;parentId&quot;:2,
              &quot;name&quot;:&quot;3&quot;
          },
          {
              &quot;id&quot;:151,
              &quot;parentId&quot;:148,
              &quot;name&quot;:&quot;4&quot;
          },
          {
              &quot;id&quot;:152,
              &quot;parentId&quot;:151,
              &quot;name&quot;:&quot;5&quot;
          }
      ]
  </code></pre>
  <p>I need to make an array <code>['1','2','148','151']</code> which means the path from &quot;parentId&quot;'s to &quot;id&quot;:152 - (argument for this function).</p>
  <p>I think main logic can be like this:</p>
  <pre><code>const parentsArr = [];
  mainArr.forEach((item) =&gt; {
      if (item.id === id) {
          parentsArr.unshift(`${item.parentId}`);
      }
  </code></pre>
  <p>and the result <code>{item.parentId}</code> should be used to iterate again. But I don't understand how to do it...</p>