【问题标题】:Does pushing Json/ajax information into an array create an object? [duplicate]将 Json/ajax 信息推送到数组中会创建一个对象吗? [复制]
【发布时间】:2017-01-21 12:41:34
【问题描述】:

此 twitch 应用程序存在问题,如果您检查控制台,它会记录一个空数组,但该数组包含具有正确信息的对象。我在数组上记录 typeof 并显示对象。我很困惑,我试图将名称推送到一个数组中,这样我就可以在随后的 for 循环中使用它们。

$(document).ready(() => {

  let followerNames = [];

    $.ajax({
    type: 'GET',
    asynx: false,
    url: "https://api.twitch.tv/kraken/users/SCKADOOSH/follows/channels",
    headers: {
       'Client-ID': 'drj8nyih5rn8z2go1x0fgga6dmudwx'
    },
    success: (data2) => {

        for (var i = 0; i < data2.follows.length; i++) {  //check number of folowers user has
          const usersCurrentFollowers = data2.follows[i]; //check the array and get the channel names of followers
          followerNames.push(usersCurrentFollowers.channel.display_name); //push that channel name into an array of user's followers
        }
    }
  });

  console.log(followerNames);



    for (var j = 0; j < followerNames.length; j++) { //iterate over the array of follower names
        $.ajax({
        type: 'GET',
        url:  'https://api.twitch.tv/kraken/streams/'+followerNames[j],
        headers: {
         'Client-ID': 'drj8nyih5rn8z2go1x0fgga6dmudwx'
        },
        success: (data) => {  
            // console.log(data);

            let streamStatus = data.stream; //status of the users stream offline/online
            // console.log(channelUrl);

            if (streamStatus === null) { //check if streaming or not....if not streaming
            const offlineChannelLogo = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/User-offline.svg/1024px-User-offline.svg.png"; //placeholder logo for offline streamer
            $("#status").append("<h3> Offline </h3>") //set status to offline
            $("#logo").append("<img class='streamerLogo' src='" + offlineChannelLogo + "'/>"); //logo to placeholder logo
            $("#streamerName").append("<h3>User Offline</h3>"); //set current stream/game to 'User Offline'

        } else { //if currently online streaming
            const channelLogo = data.stream.channel.logo; //get channel logo
            const streamerName = data.stream.channel.display_name; //get streamer name
      const streamLink = "https://www.twitch.tv/"+streamerName; //get link to streamers schannel
            $("#status").append("<h3><a href=" + streamLink +">" + streamStatus.game + "</a></h3>") //display game currently streaming on channel
            $("#logo").append("<img class='streamerLogo' src='" + channelLogo + "'/>"); //display channel logo
            $("#streamerName").append("<h3>"+ streamerName+"</h3>"); //display streamer name
        }
    }
    });
   }
});

【问题讨论】:

  • 披萨送达前不能吃。 Ajax 是异步的

标签: javascript jquery arrays ajax for-loop


【解决方案1】:

数组对象。几乎一切都是。更多关于 here.

至于console.log(),您在发出 ajax 请求后登录,而不是在结果出现后登录,这就是您得到一个空数组的原因 - 因为还没有数据。您应该将日志放在 success 回调中以便记录数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2013-04-11
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多