我正在尝试在javascript中创建一个对象数组,但是当我尝试将数组打印到控制台(使用chrome)时,我得到的只是未定义,除非我在推送之后打印数组。
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