[英]How to iterate through a list in javascript
Server returns a list of data, and I want to iterate through the list. 服务器返回数据列表,我想遍历该列表。
Here is how the data is structured (got it from browser debugger): 数据的结构如下(从浏览器调试器中获取):
And the function: 和功能:
function (token) {
hub.server.getOnlinePlayers(token).done(function (onlinePlayers) {
MyData.nextOnlinePlayersToken = onlinePlayers.Token;
$.each(onlinePlayers.Items, function () {
var id = this.userId;
});
});
}
until this line everything works fine ( Token
value is null intentionally): 直到这一行一切正常为止(
Token
值故意为空):
MyData.nextOnlinePlayersToken = onlinePlayers.Token;
But the next line the debugger shows that onlinePlayers
is undefined. 但是调试器的下一行显示未定义
onlinePlayers
。 What possibly is wrong? 可能是什么问题? Thanks.
谢谢。
I think you are mistaken $.each() and $(selector).each() 我认为您错了$ .each()和$(selector).each()
$( "li" ).each(function( index ) {
// here you can access current li element with this.
});
You are trying to get the jQuery object which calls each, but you don't have it. 您正在尝试获取调用每个对象的jQuery对象,但是您没有它。 I believe in this case
this
is the global jQuery object. 我相信在这种情况下,
this
是全局jQuery对象。
You need to pass index and value arguments if you are using the $.each()
如果使用
$.each()
则需要传递索引和值参数。
$.each(onlinePlayers.Items, function (index, value) {
var id = value.userId;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.