0

I am having trouble accessing specific values of an object I am creating from an API call. Essentially, I want to console log a specific object value, but all I'm getting is undefined. Here's my code and a link to my project on codepen:

$(document).ready(function() {
   var users = ["freecodecamp", "trymacs", "phonecats"];
   var info = {};
   var i;
   var identifier;

   for (i = 0; i < users.length; i++) {
      $.getJSON("https://wind-bow.gomix.me/twitch-api/streams/" + users[i], function(json) {
         identifier = json["_links"].self;
         identifier = JSON.stringify(identifier);
         identifier = identifier.substr(identifier.lastIndexOf('/') + 1);
         identifier = identifier.slice(0, -1);

         info[identifier] = JSON.stringify(json);
      });
   }
   console.log(info.freecodecamp);

});

Also, note that if you just log info the whole object looks fine.

Thanks in advance for any and all help!

Hudson Taylor
  • 1,142
  • 2
  • 10
  • 30
  • The dup + also good reading: http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Teemu Jan 04 '17 at 20:01
  • the response to getJSON is asynchronous so it has not returned when you try to log it – Pabs123 Jan 04 '17 at 20:01
  • This is a more specific case of async returns, particularly because he wants MANY calls to return. I have a good solution using promises which I think will be helpful, i think this was prematurely closed as it isn't exactly a duplicate @daniel-a-white – Pabs123 Jan 04 '17 at 20:13
  • http://codepen.io/pabs123/pen/apzvqN @H. Allen if you still wanna see how it's done – Pabs123 Jan 04 '17 at 20:21

0 Answers0