I am trying getting data from an api but the For-loop that i was using was returning objects with null and undefined values in the nested GetJSON call but when i tried forEach loop it works fine. Any ideas why?
var baseUrl = "https://wind-bow.gomix.me/twitch-api/";
var channels = ["ESL_SC2", "FreeCodeCamp", "Test_channel", "brunofin"];
//**First function with forEach. WORKS FINE**
function getDataForEach() {
channels.forEach(function(channel) {
$.getJSON(txtUrl('streams', channel), function(json) {
console.log(json);
$.getJSON(txtUrl('channels', channel), function(json2) {
console.log(json2);
});
});
});
}
//**Second function with a normal for loop. The 2nd JSON doesnt return the proper data**
function getDataForLoop() {
for (i=0;i<channels.length;i++){
$.getJSON(txtUrl('streams', channels[i]), function(json) {
console.log(json);
//THIS 2nd call doesnt returns undefined objects.
$.getJSON(txtUrl('channels', channels[i]), function(json2) {
console.log(json2);
});
});
});
}
function txtUrl(prop, chnl) {
return baseUrl + prop + '/' + chnl + '?callback=?';
}
$(document).ready(function() {
getData();
});