title: JavaScript - unable to add JSON values into array
body: <p>I have a JSON file with informations of a lot of characters, but I only need one specific information of 5 characters and put the info into an array, but when I try to access data in the array it returns undefined. The needed info of each character is an array.</p>
  <p>The rough format of JSON file:</p>
  <pre><code>{Version: &quot;10.2.1&quot;
   data: {
     a: {neededInfo:[&quot;An array of needed info&quot;], otherInfo:&quot;any other info&quot;}
     b: {neededInfo:[&quot;An array of needed info&quot;], otherInfo:&quot;any other info&quot;}
     ...
   }
  }
  </code></pre>
  <p>Below is my first attempt:</p>
  <pre><code>//a given array with character names
  team = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]
  var info = [];
  
  for (let i=0; i&lt;5; i++) {
      const name = team[i]
  
      fetch('characters.json')
          .then((response) =&gt; response.json())
          .then((response) =&gt; response.data)
          .then((response) =&gt; {
              info.push(response[name][&quot;neededInfo&quot;])
          })
          .catch((error) =&gt; console.error(error))
  }
  </code></pre>
  <p>when I console.log(info) it gave me this:
  <a href="https://i.stack.imgur.com/GbEce.png" rel="nofollow noreferrer">console.log(info)</a></p>
  <p>I researched and suspected that it might be something related to async/await, so I changed the code to this:</p>
  <pre><code>//a given array with character names
  team = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]
  var info = [];
  
  const fetching = async () =&gt; {
      const response = await fetch('characters.json')
      const responded = await response.json()
      const data = await responded.data
      for (let i=0; i&lt;5; i++) {
          const name = team[i]
          info.push(data[name][&quot;neededInfo&quot;])
      }
  }
  
  </code></pre>
  <p>This worked as well, but it produces the same array as the first attempt. I am unable to get array elements via index, and the length of the array is 0. Any reasons why is this happening?</p>
  <p>Any help will be appreciated.</p>