【问题标题】:Javascript array not returning the correct lengthJavascript数组没有返回正确的长度
【发布时间】:2011-06-15 09:04:48
【问题描述】:

这可能是非常愚蠢的事情,但对于我的生活我看不到它。

我有用字符串填充数组的代码,然后我想循环遍历数组并将值作为选项添加到选择控件中。但是由于某种原因,当我调用长度属性时它返回 0,但是如果我使用 console.dir 在控制台中显示数组,它里面有数据。

代码如下:

var sectors = new Array();
// code to add values 
$.getJSON("get/tech_info.php", {uid:json.uid}, function(data){

    $.each(data, function(i, json){
        if($.inArray(json.area, sectors) == -1)
        {
            sectors[sectors.length] = json.area;
        }
    });
});


console.dir(sectors);

var sec = "#mainSectors" + count;
console.log(sectors.length); // returning 0
for ( var i=0; i != sectors.length; i++ ){

    console.log("bla bla bla");
    $(sec).append(
        $("<option></option>").html(sectors[i])
    );
}

这是你可以看到的 cosole.dir 数据有数据:

//secotrs
Array[0]
0: "Industrial"
1: "Commercial"
length: 2
__proto__: Array[0]

【问题讨论】:

  • 你能显示你实际使用console.dir的代码吗?以及添加值的代码?
  • 我认为问题出在// code to add values goes here //
  • 我看到你的数组是多维的
  • 如果我去扇区[0].length它会抛出一个错误,这只是console.dir显示数据的方式
  • 错误是什么?如果sectors[0] = "Industrial",那么sectors[0].length 应该显示10(字符串的长度)。

标签: javascript


【解决方案1】:

您正在进行 Ajax 调用。 Ajax 是异步的!调用之后的代码(for 循环)在执行回调之前 执行(并且填充数组)。

将所有依赖sectors的代码放入回调中:

var sectors = [];

$.getJSON("get/tech_info.php", {uid:json.uid}, function(data){

    $.each(data, function(i, json){
        if($.inArray(json.area, sectors) == -1)
        {
            sectors.push(json.area);
        }
    });

    var sec = "#mainSectors" + count;
    console.log(sectors.length); //
    for ( var i=0; i != sectors.length; i++ ){

        console.log("bla bla bla");
        $(sec).append(
            $("<option></option>").html(sectors[i])
        );
    }

});

更新:

关于console.dir的输出:和我猜的一样。对象的属性是在您展开控制台中的条目时获取的,而不是在您进行调用时获取的。

例子:

> a = [];
  []
> console.dir(a)
  ▸ Array[0]
    undefined
> a.push(5)
  1

当我现在单击 Array[0] 旁边的标记时,我得到:

▾ Array[0]
   0: 5
   length: 1
  ▸ __proto__: Array[0]

人们期望它确实显示0: 5,因为它是后来添加的(在console.dir 调用之后)。

【讨论】:

  • 但我在尝试遍历数组之前调用了console.dir,并且console.dir 有数据。
  • @MattBH:我不确定那个。但是你可以看到它打印了Array[0],它应该是Array[2]。事实上,for循环会在收到响应之前运行。
  • 如果你再往下看一点,你可以看到长度属性 = 2
  • @Matt:我知道。也许这些值是在您单击那个小标记以列出对象的属性时获取的,而不是在您实际调用console.dir 时获取的。当您单击它时,Ajax 调用很可能已经返回。
  • 我不知道如何或为什么,但你是对的。我错了。谢谢一百万!
【解决方案2】:

您正在显示length 属性(带有console.log之前 一些东西被添加到数组中,所以我认为0 是正确的长度。之后,您将循环一个空数组。

此外,如果扇区有length,那么for ( var i=0; i != sectors.length; i++ ) 可以无限运行,因为当它不是sectors.length 时它会跳过i 并继续前进。使用i&lt;sectors.length

【讨论】:

  • 我已粘贴代码以将值添加到问题中。但它甚至没有进入 for 循环
  • 好的,我知道你有答案了。工作太忙了,没看下去,抱歉。
【解决方案3】:

我想对于这一行 sectors[sectors.length] = json.area; sectors 超出范围 var sectors = new Array(); 应该在任何函数之外的主范围内

【讨论】:

  • 只要sectors 在相同或更高的范围内就可以了。
  • 这是代码的复制粘贴,扇区在函数调用之前声明并初始化
  • 但是 Felix Kling 得到了答案,我认为它可能超出了范围,但它实际上是不同步的。
【解决方案4】:

不应该如下:

  if($.inArray(json.area, sectors) == -1)

成为

  if($.inArray(json.area, sectors) != -1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 2012-11-23
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2019-04-23
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多