title: While mapping, how to access to Object keys inside loop? Angular
body: <p>I have an array that contains all the keys that are supposed to be sent, the problem comes when I try to invoke them inside a for loop that is within a map, I get an error:</p>
  <blockquote>
  <p>Cannot read property '0' of undefined</p>
  </blockquote>
  <p>If I print the array keys they will print well, but I can not point to them in a nested JSON array. Let me show what I mean</p>
  <p>This is my array that contains all the keys</p>
  <pre><code>reportFieldsArr = [
  0: &quot;PrefixTitle&quot;
  1: &quot;Company&quot;
  2: &quot;Address&quot;
  3: &quot;Zip&quot;
  ]
  </code></pre>
  <p>I will pass that array to a function that will try to access every key in a <code>map</code></p>
  <p>When I try to do this is when I received the error, because can not access to <code>.reportFieldsArr[index]</code>:</p>
  <pre class="lang-js prettyprint-override"><code>leads.find(s =&gt; s.personId=== id).reportFieldsArr[index]
  </code></pre>
  <p>Here it is the all function</p>
  <pre class="lang-js prettyprint-override"><code> filterLeads(leads,reportFieldsArr){ 
      this.leads = Array.from(new Set(leads.map(s =&gt; s.personId)))
      .map(id =&gt; {
        let leadFields:any = []
        for(let index = 0; index &lt; reportFieldsArr.length; index++){
          leadFields[index] = leads.find(s =&gt; s.personId=== id).reportFieldsArr[index]
        }
        
        return leadFields
      });
    }
  </code></pre>
  <p>This is how is working right now, but it is with static data, I needed to be dynamic:</p>
  <pre class="lang-js prettyprint-override"><code>filterLeads(leads,reportFieldsJSON,reportFieldsArr) { 
      this.leads = Array.from(new Set(leads.map(s =&gt; s.personId)))
      .map(id =&gt; {
        let leadFields:any =  {
          &quot;PrefixTitle&quot;: leads.find(s =&gt; s.personId=== id).PrefixTitle,
          &quot;Company&quot;: leads.find(s =&gt; s.personId=== id).Company,
          &quot;Address&quot;: leads.find(s =&gt; s.personId=== id).Address,
          &quot;Zip&quot;: leads.find(s =&gt; s.personId=== id).Zip,
        };
        
        return leadFields
     });
  }
  </code></pre>