0

var channelsData = [];

//let's assume i have here 3 objects from api calls here

How I can loop through this and get the objects.I tried with

for

loop and

forEach

but still got nothing and if i try to get channelsData.length , i get 0.

If i try to console.log channelsData i get this [in the console][1]

[1]: https://i.stack.imgur.com/TkMLS.png Code:

    $(document).ready(function() {
  var channels = ["andreiknight", "freecodecamp","creativemonkeyz"];
  var channelsData = [];

  var channelsData = [];

  channels.forEach(function(channel) {
    $.getJSON("https://wind-bow.glitch.me/twitch-api/channels/" + channel + "", function(data){
      //console.log(data);
      channelsData.push(data);
  });
});
  channelsData.forEach(function(channel){
    console.log(channel); // returns nothing in the console
  })



});
Andr3i
  • 11
  • 3
  • post your js code – tech2017 Jun 12 '17 at 16:51
  • 1
    Probably got something to do with [**this problem**](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – ibrahim mahrir Jun 12 '17 at 16:54
  • **Washington Guedes** it's not working, so i am having a call from an api and i push the data to channelsData . – Andr3i Jun 12 '17 at 16:55
  • Can you please post your specific code? Just typing 'for' is unhelpful. We can all guess as to what your problem is but it would be impossible to know for sure without seeing the actual code. – rob Jun 12 '17 at 16:56
  • We need to see your code. It is hard to guess, but probably an asynchronows issue. – Washington Guedes Jun 12 '17 at 16:56
  • sure wait ` $(document).ready(function() { var channels = ["andreiknight", "freecodecamp","creativemonkeyz"]; var channelsData = []; channels.forEach(function(channel) { $.getJSON("https://wind-bow.glitch.me/twitch-api/channels/" + channel + "", function(data){ channelsData.push(data); }); }); channelsData.each(function(channel){ console.log(channel); }) }); ` – Andr3i Jun 12 '17 at 16:57
  • use the [edit](https://stackoverflow.com/posts/44504730/edit) button under your question to edit your code into your question. Do not try posting it in comments as there is not enough room and is not readable – Patrick Evans Jun 12 '17 at 17:09
  • I did it. ----- – Andr3i Jun 12 '17 at 17:14
  • `$.getJSON` is an asynchronous call. It means your javascript code will not wait it, because it can take too long to return your data. – Washington Guedes Jun 12 '17 at 17:18
  • Said that, your `channelsData.forEach` code must be inside the callback of `$.getJSON` – Washington Guedes Jun 12 '17 at 17:18
  • Thanks , i got it. – Andr3i Jun 12 '17 at 17:20
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ibrahim mahrir Jun 12 '17 at 17:25
  • @ibrahimmahrir yes thank you, thanks all for helping. – Andr3i Jun 12 '17 at 17:28

0 Answers0