返回数组的Javascript长度为0 [英] Javascript length of returned array is 0

查看:99
本文介绍了返回数组的Javascript长度为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从steam获取一个JSON对象并将steamID放在一个数组中。之后,我返回数组,以便我可以在其他功能中使用它。但是我非常沮丧,以至于我退回后无法使用数组。

我也无法循环播放数组..但是当我检查控制台时,我可以看到那个里面有一些元素,但是我无法以某种方式访问​​它...



I'm fetching a JSON object from steam and put the steamID's in an array. After that i return the array so i can use it in other functions. But i'm so frustrated that I can't work with the array after i return it.
Also i can't loop through the array.. But when i check console, i can litterally see that there is some array with elements inside but i just can't access it somehow...

function example(){
var friendList = getFriendsOfUser(steamID);
console.log(friendList.length); // returns 0
}

function getFriendsOfUser(steamID){
    var steamFriendIDs = new Array();

    if (steamID == null || steamID == "") {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid=76561197960435530&relationship=friend";
    }
    else {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid="+steamID+"&relationship=friend";
    }

    $.ajax({
      dataType: "json",
      url: url,
      success: function(data){
        $.each(data["friendslist"]["friends"], function(index, value){
          steamFriendIDs.push(data["friendslist"]["friends"][index]["steamid"]);
        });
      }
    });

    return steamFriendIDs;
  }





我的尝试:



只返回JSON对象不起作用,因为它显示未定义。

我这样做的方式确实给出了一个包含元素的数组,但没有任何作用。



我尝试访问它:



What I have tried:

Returning just the JSON object doesn't work, because it shows undefined.
The way i did it does give an array with elements inside, but nothing works.

I tried to access it like:

function example(){
var friendList = getFriendsOfUser(steamID);
console.log(friendList[0].length); // cannot read property length of undefined...
}

推荐答案

.ajax({
dataType: json
url:url,
成功: function (数据){
.ajax({ dataType: "json", url: url, success: function(data){


.each(data [ friendslist] [ friends], function (index,value){
steamFriendIDs.push(data [ friendslist] [ friends] [index] [ steamid]);
});
}
});

return steamFriendIDs;
}
.each(data["friendslist"]["friends"], function(index, value){ steamFriendIDs.push(data["friendslist"]["friends"][index]["steamid"]); }); } }); return steamFriendIDs; }





我的尝试:



只返回JSON对象不起作用,因为它显示未定义。

我这样做的方式确实给出了一个包含元素的数组,但没有任何作用。



我尝试访问它:



What I have tried:

Returning just the JSON object doesn't work, because it shows undefined.
The way i did it does give an array with elements inside, but nothing works.

I tried to access it like:

function example(){
var friendList = getFriendsOfUser(steamID);
console.log(friendList[0].length); // cannot read property length of undefined...
}


AJAX代表异步 Javascript和Xml。关键词是异步 -
AJAX stands for Asynchronous Javascript And Xml. The key word there is "Asynchronous" - the


这篇关于返回数组的Javascript长度为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆