javascript - Why can an array with 3 object elements only access the first element?
PHP中文网
PHP中文网 2017-05-19 10:23:15
0
4
463


There are three elements in status expansion
Then status[0] is the object element I push in
Then status [1] and status[2] are all kinds of undefined, why?

The following is the complete code. After trying it several times, I think it has something to do with the variable scope. However, the array variable result is in the outermost layer. Why can't push() inside it be retrieved from the outside?

function getStreamsStatus(channels) {
  var results = [];
  $.each(channels, function(index, channel) {
    var result = [];
    // result.push({ name: channel });
    $.getJSON (
      "https://api.twitch.tv/kraken/streams/" + channel,
      {
        Accept: "application/vnd.twitchtv.v5+json",
        client_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        callback: ""
      },
      function(data) {
        result.push(data);
        $.getJSON(
          data._links.channel,
          {
            Accept: "application/vnd.twitchtv.v5+json",
            client_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            callback: ""
          },
          function(channel_data) {
            result.push(channel_data);
            // console.log(result);
            // console.log(result[0]);
            // console.log(result[1]);
          });
        console.log(result);
        console.log(result[0]);
        console.log(result[1]);
      });
    results.push(result);
    // console.log(result);
    // console.log(result[0]);
    // console.log(result[1]);
  });
  return results;
}
PHP中文网
PHP中文网

认证0级讲师

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!