0

The following javascript endpoint contains list of cities in United States. I am pushing the elements to the array citiesInfo after I retrieve it from the endpoint. The array shows up when I do console.log(citiesInfo); and I can navigate through the array in console but when I do console.log(citiesInfo[0]); It shows undefined

const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
const citiesInfo = [];
fetch(endpoint)
  .then(function(elem){
    return elem.json();
   })
.then(function(elem){
   citiesInfo.push(...elem);
 });
paris_serviola
  • 361
  • 1
  • 6
  • 19
  • Check the type. It's probably an object representing a collection of items, rather than a native array. – Mr. Polywhirl Jan 20 '17 at 15:38
  • 1
    http://jsbin.com/nicuda/1/edit?js,console — I can't reproduce the problem (but while you say you are using console.log, you didn't provide an [mcve] showing that, so it is hard to say what you might be doing wrong) – Quentin Jan 20 '17 at 15:40

0 Answers0