Can';t迭代或查找复杂Javascript数组的长度

Can';t迭代或查找复杂Javascript数组的长度,javascript,arrays,Javascript,Arrays,我有一个复杂的Javascript数组,当写入控制台时如下所示: function Standing(flagsmall, teamname, won, drawn, lost, goalsfor, goalsagainst, points) { this.flagSmall = flagsmall; this.teamName = teamname; this.won = won; this.drawn = drawn;

我有一个复杂的Javascript数组,当写入控制台时如下所示:

function Standing(flagsmall, teamname, won, drawn, lost, goalsfor, goalsagainst, points) {
        this.flagSmall = flagsmall;
        this.teamName = teamname;
        this.won = won;
        this.drawn = drawn;
        this.lost = lost;
        this.goalsFor = goalsfor;
        this.goalsAgainst = goalsagainst;
        this.points = points;
    }

 var arrStandings = new Array();

serviceUrl = "http://cloudapp.net/odata/Standings?$expand=Team,Stage&$filter=Team/Group/GroupName eq 'A'";
        var line = "";

        $.getJSON(serviceUrl, function (results) {

            $.each(results['value'], function (i, item) {

                //calculate points
                var points = (item.Won * 3) + (item.Drawn * 1);

                var standing = new Standing(item.Team.FlagSmall, item.Team.TeamName, item.Won, item.Drawn, item.Lost, item.GoalsFor, item.GoalsAgainst, points);

                arrStandings.push(standing);
                arrStandings.sort(sortfunction);
            });
        });

但是,当我尝试遍历它时,我不能,当我检查它的长度时,它返回0:

console.dir(arrStandings.length);

    arrStandings.forEach(function (index) {

        ...
    });

如何访问数组中的对象?

初始化数组时,我遇到了类似的问题,但我放置了一个字符串键,数组被强制转换为对象。返回数组/对象的此代码长度

Object.keys(myArray).length
您可以使用for语句迭代对象:

for ( index in arrStanding ){
    // your code
}
编辑:

问候,,
Kevin

代码中没有100%的清晰信息,但看起来您正在尝试在填充数组之前打印数组的长度


$.getJSON是一个异步操作,因此在写入控制台时,尚未填充它


javascript控制台中显示的表示将在数组创建后(首次访问时)呈现

我们需要更多关于这个数组是如何创建的信息,而不仅仅是
新数组
非常奇怪-你没有告诉我们代码:你的控制台屏幕上显示的信息是数组的长度是
4
。请查看上面的编辑中数组是如何创建的。你确定从Json文件中获取数据吗?添加了成功:回拨并查看得到的结果和错误:$.getJSON是一个异步操作,当您将长度写入控制台时,它尚未填充。javascript控制台中显示的表示形式是在数组创建后,即首次访问数组时呈现的。它与jQuery中的每个表示形式都相同。这正是我所寻找的答案,通过在getJSON末尾添加一个done()来修复
//  here you have an array
arrStandings.push(standing);
// ...and after this line?
arrStandings.sort(sortfunction);