title: How to iterate an array of objects recursively in TypeScript/Javascript
body: <p>I have an array with objects that contains two properties: <code>target</code> and <code>source</code>. Moreover, I have a <code>starting source</code>. </p>
  
  <p>Now what I need to start with <code>starting source</code> and find a <code>target</code> from the <code>array</code>. After finding the <code>target</code>, that value becomes <code>source</code> again find a <code>target</code> from that <code>array</code>. It should run recursively until target returned as undefined. </p>
  
  <p>What is the efficient way to do this?</p>
  
  <p>I have implemented this problem as followed. Some short and crisp code suggestion is required. </p>
  
  <pre><code>let result = new Array();
  // input
      const a = [
        { source: '2', target: '3' },
        { source: '1', target: '2' },
        { source: '3', target: '4' },
        { source: '4', target: '5' }
      ];
      const startSource = '1';
      result.push(startSource);
     // recursive function
      this.findTarget(startSource, a);
      console.log(result);
  
  </code></pre>
  
  <p>Following is the recursive function </p>
  
  <pre><code> public findTarget(s: string, a: Sequence[]): any {
      let obj = a.find(i =&gt; i.source === s);
      if (obj) {
        this.result.push(obj.target);
        return this.findTarget(obj.target, a);
      } else {
        return;
      }
    }
  
  </code></pre>