Hi campers. I’m currently working on Twitch.tv JSON API
project and trying to sort()
my channels alphabetically. It works fine in console but with every page reload it sorts differently. I guess it has something to do with the for loop
I’m using. But I cannot figure out what exactly.
I would appreciate any hint or help! Thanks
Project-link https://codepen.io/dknvmlhok/full/eyzgVm
$(document).ready(function(){
let searchVal = $("#search").val();
let apiUrl = "https://api.twitch.tv/kraken/channels/"+searchVal;
let initialChannels = ["freecodecamp",
"ESL_SC2",
"OgamingSC2",
"cretetion",
"storbeck",
"habathcx",
"RobotCaleb",
"noobs2ninjas"];
let sortedChannels = initialChannels.sort();
console.log(sortedChannels);
/*******INITIAL CHANNELS AJAX*******/
for(let i = 0; i < sortedChannels.length; i++){
$.ajax({
type: 'GET',
url: apiUrl +sortedChannels[i],
headers: {
'Client-ID':'aj6k9bb6snp76o59babzqeobxbbcgs'},
success: function(data){
$('<div id="channel-wrap">'
+'<img src='+data.logo+' alt="Channel logo" id="logo">'
+'<a href='+data.url+' target="_blank" id="channel-name">'+data.display_name+'</a>'
+'</div>'+'</br>').appendTo("#card-body");
}
});
};