需要帮助将来自 JSONP 调用的多个数组放入一个数组中

Need help putting several arrays from a JSONP call into one single array

我正在尝试将抽搐上某个频道的所有观众(聊天中的人)放入一个数组中。

JSONP 调用有效,但 Twitch API returns 几个数组:

我想将这些数组中的所有用户名放入一个数组中(即 "peopleinchat")。

我的方法是先声明数组:

var peopleinchat= [];

然后使用 $.merge() 将每个后续数组(组)的人添加到此数组。但是我无法让它工作...我得到 "TypeError: Cannot read property 'length' of undefined"。你能帮帮我吗?

谢谢!

$.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>

差不多了 - 我认为您的主要问题是您没有处理 result 参数到 success 回调的正确形状。下面的代码片段有效:

$.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.data.chatters, function(index, data){

        $('.viewers').append(index + ': ' + data);
        $('.viewers').append('<br>');
        
        $.merge(peopleinchat, data);

    });
    
    $('.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>