【发布时间】:2016-08-06 11:45:36
【问题描述】:
我正在尝试将 twitch 上某个频道的所有观众(聊天中的人)放入一个数组中。
JSONP 调用有效,但 Twitch API 返回几个数组:
- 版主
- 员工
- 管理员
- global_mods
- 观众
我想将这些数组中的所有用户名放入一个数组中(即“peopleinchat”)。
我的做法是先声明数组:
var peopleinchat= [];
然后使用$.merge() 将每个后续数组(组)添加到此数组中。但是我无法让它工作......我得到“TypeError:无法读取未定义的属性'长度'”。你能帮帮我吗?
谢谢!
$.ajax({
url: "https://tmi.twitch.tv/group/user/batuhanbuyukakkan/chatters",
// The name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
// Work with the response
success: function( response ) {
var peopleinchat = [];
$.each(response, function(index, data){
$.each(data.chatters, function(index, group){
$('.viewers').append(index + ': ' + group);
$('.viewers').append('<br>');
$.merge(peopleinchat, group);
});
});
$('.array_result').append(peopleinchat);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="viewers"></div>
<br>
<br>
<div class="array_result"></div>
【问题讨论】:
-
我不明白为什么我会收到这个
TypeError: Cannot read property 'length' of undefined。它似乎与将不同的数组放入一个更大的数组无关,但它确实阻止了代码比最后一个每个循环执行得更远:S