I have got an array of channel names(user_arr) and I need to fetch their details from twitch.tv
Here is the code please read the details below
$(document).ready(function(){
var user_arr = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]
var newarr =[];
var streams = [];
var channels = [];
for(var i = 0 ; i < user_arr.length; i++){
name = user_arr[i];
var url1 ="https://wind-bow.gomix.me/twitch-api/streams/"+name+"?callback=?"
var url2 ="https://wind-bow.gomix.me/twitch-api/channels/"+name+"?callback=?"
$.ajax({
type:"GET",
url:url1,
dataType:"json",
async:false,
success:function(streamed_data){
streams.push(streamed_data);
}
});
$.ajax({
type:"GET",
url:url2,
dataType:"json",
async:'true',
success:function(channel_data){
channels.push(channel_data);
}
});
}
console.log(channels);
console.log(channels[1]);
});
As you can see from the code I am trying fetch the details in json but apparently when I push the details into a new array they are not being pushed in the same manner as the user_arr. I believe they are being pushed in order of whichever is received first
I am using jquery and have asked many people regarding this issue but most of the replies were regarding promises (a concept I don't understand and don't know how to use it)
Plus when I console.log(channels); it is showing me the array filled with data but when I console.log(channels.length) right after the previous command the length is 0 and no data is being showned
Can I know what is the best way to get the data in the order of the original array using $.ajax as well as $.getJSON
I tried to apply the map function to the final array to rearrange the array but failed
It would be really helpful if someone could show me how we can apply the map function to the array in order to get the final array based on the user_arr array