-1

i have a problem with reading objects from an array. I can console.log(objArr), but console.log(objArr[0]) = undefined. pic from codepen

var arr = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var content = document.querySelector(".content");
var objArr = [];


  arr.forEach(function(data, index){
    $.getJSON("https://wind-bow.gomix.me/twitch-api/streams/" + data + "?callback=?", function(info) {
    if(info.stream){
              objArr.push({
            name: data,
            status: "online",
            link: info._links.channel,
            game: info.stream.game
              });
   } else{
              objArr.push({
            name: data,
            status: "offline",
            link: info._links.channel,
            game: "offline"
              });
    }
   });
  })

console.log(objArr[0]);

Can anyone help me?

  • jQuery's [*each*](http://api.jquery.com/each/) passes values to the callback in a different order to the ECMAScript [*forEach*](http://ecma-international.org/ecma-262/8.0/#sec-array.prototype.foreach). jQuery passes the index first, then the value so in your code, *data* is the index and *index* is the value. jQuery also doesn't pass the object or allow a value to be passed as *this*. – RobG Dec 23 '17 at 13:10

1 Answers1

0

I assume your console.log is called too early. To get the json takes some time. Try to put you log after the if(info.stream){}else{}