起初,我在努力使数组排序,但是我进行了一些测试,发现问题似乎在于数组返回的是未定义的。
var channels = ["BalticSurvivor", "Daddere", "Desudesugaming", "Doublevisionss", "Heyshadylady", "Josephtho", "Jsstudz", "Lady_ocelot", "Magalian", "Nightwake", "Nostrus", "Nvmkthnxbai", "ShadowSlayer52", "Superxinvader", "Theadman2315", "Unic0rny", "Wiigytv"];
var cID = ""
var arrayOffline = [];
var arrayOnline = [];
$(document).ready(function () {
channels.forEach(function (channel) {
$.ajax({
type: 'GET',
url: 'https://api.twitch.tv/kraken/streams/' + channel,
headers: {
'Client-ID': cID
},
success: function (data) {
if (data.stream != null) {
var dumbyarray = [channel, data.stream.preview.medium, data.stream.channel.game, data.stream.channel.status, data.stream.viewers, data.stream.channel.followers];
arrayOnline.push(dumbyarray);
} else {
$.ajax({
type: 'GET',
url: 'https://api.twitch.tv/kraken/channels/' + channel,
headers: {
'Client-ID': cID
},
success: function (odata) {
var dumbyarray = [channel, odata.logo, odata.game, odata.status, odata.followers];
arrayOffline.push(dumbyarray);
}
});
}
}
});
});
console.log("Offline");
console.dir(arrayOffline);
console.log("arrayOffline type: " + typeof arrayOffline[0])
console.log("Online");
console.dir(arrayOnline);
console.log("arrayOnline type: " + typeof arrayOnline[0])
});
数据正在返回: