我试图让一些直播流的在线状态和观众人数。我从AJAX获取来自我网站的对象数组。然后我遍历数组并向这些对象添加值。问题是,当我在控制台中记录这些对象的内容时,我发现新创建的密钥,但是当我尝试记录该密钥的值时,它说它是未定义的。jQuery操纵对象内.each()
$.each(livestreams, function() {
if(this.provider === 'TwitchTV') {
$.ajax({
url: 'http://api.justin.tv/api/stream/list.json?channel=' + this.channel.toLowerCase(),
dataType: 'jsonp',
jsonp: 'jsonp',
success: $.proxy(function (stream) {
this.live = true;
this.count = stream[0].channel_count;
}, this)
});
} else {
$.ajax({
url: 'http://api.own3d.tv/liveCheck.php?live_id=' + this.channel,
dataType: 'xml',
success: $.proxy(function (stream) {
this.live = stream.find('isLive').text();
this.count = stream.find('liveViewers').text();
}, this)
});
}
console.log(this);
/* returns
channel: "garenatw"
count: 8237
id: "3"
live: true
name: "garena"
provider: "TwitchTV"
username: "grifon"
__proto__: Object
(of course, only for this specific object)
*/
console.log(this.live); // returns undefined
});
你可以发布[jsfiddle](http://jsfiddle.net/)吗? –
http://jsfiddle.net/almirsarajcic/C4Jur/ 对不起,我试图用HTML格式化它,但它没有按照我的预期工作。 –
欢迎来到** async **的精彩世界!你不能那样做。 – SLaks