I am attempting to make an array of objects in javascript but when i try and print the array to the console (using chrome) all i get is undefine, unless i print the array just after the push.
var subitems = [];
for (var i = 1; i < 1000; i = i + 50) {
$.getJSON('http://gdata.youtube.com/feeds/api/users/' + username + '/subscriptions?max-results=50&start-index=' + i + '&alt=json', function (data) {
var output = "";
for (var x in data.feed.entry) {
var subtitle = data.feed.entry[x].yt$username.$t;
var subchannel = data.feed.entry[x].yt$channelId.$t;
var subphoto = data.feed.entry[x].media$thumbnail.url;
subitems.push({
subtitle: subtitle,
subchannel: subchannel,
subphoto: subphoto
});
console.log(subitems[0]); //Prints Fine
output += '<div class="subtitle"><img src="' + subphoto + '"> <a href="http://www.youtube.com/channel/' + subchannel + '">' + propercase(subtitle) + '</a></div>';
}
maincontent.innerHTML = maincontent.innerHTML + output;
});
}
console.log(subitems[0]); //This is undefined