2017-10-06 1 views
0

서버는 데이터 목록을 반환하고 목록을 반복합니다. 여기자바 스크립트에서 목록을 반복하는 방법

데이터가 어떻게 구성되어 있는지입니다 (브라우저 디버거에서 그것을 가지고) :

enter image description here

그리고 기능 :

function (token) { 
    hub.server.getOnlinePlayers(token).done(function (onlinePlayers) { 
     MyData.nextOnlinePlayersToken = onlinePlayers.Token; 
     $.each(onlinePlayers.Items, function() { 
      var id = this.userId; 
     }); 
    }); 
} 

을이 라인의 모든 것이 잘 작동 할 때까지 (Token 값은 null입니다 의도적으로) :

MyData.nextOnlinePlayersToken = onlinePlayers.Token; 

그러나 다음 줄은 디버거가 onlinePlayers이 정의되지 않았 음을 보여줍니다. 무엇이 잘못된 것일까 요? 감사.

답변

2

은 당신이 당신은 각 호출 jQuery 오브젝트를 얻기 위해 노력하고있다 $.each()$(selector).each()

$("li").each(function(index) { 
    // here you can access current li element with this. 
}); 

잘못된 생각,하지만 당신은 그것을 필요가 없습니다. 이 경우 this은 글로벌 jQuery 객체입니다. 당신은 내가 오타를 가지고 내 코드에서 $.each()

$.each(onlinePlayers.Items, function (index, value) { 
    var id = value.userId; 
}); 
+0

를 사용하는 경우

당신은 인덱스와 값 인수를 전달해야합니다. 'this.userId'는'this.UserId'이어야합니다. 이제는 잘 작동합니다. 어쨌든 고마워요. – Blendester

관련 문제