简体   繁体   English

JavaScript数组未返回正确的长度

[英]Javascript array not returning the correct length

This is probably something really stupid but for the life of me i can't see it. 这可能真是愚蠢,但对于我的一生,我看不到。

I have code that fills an array with strings and ii then want to loop through the array and add the values to as options in a select control. 我有用字符串填充数组的代码,然后ii想要遍历该数组并将值添加为select控件中的选项。 But for some reason when i call the length property it returns 0, but if i use console.dir to display the array in the console it has data in it. 但是由于某种原因,当我调用length属性时,它返回0,但是如果我使用console.dir在控制台中显示该数组,则它中包含数据。

here's the code: 这是代码:

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])
    );
}

Here's the cosole.dir data which you can see has data: 这是cosole.dir数据,您可以看到其中的数据:

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

You are making an Ajax call. 您正在拨打Ajax电话。 Ajax is asynchronous ! Ajax是异步的 The code after the call (the for loop) is run before the callback executed (and the array is populated). 调用( for循环)之后的代码在执行回调之前运行(并填充了数组)。

Put all the code that depends on sectors in the callback: 将所有依赖于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])
        );
    }

});

Update: 更新:

Regarding the output of console.dir : It is as I assumed. 关于console.dir的输出:这是我假设的。 The properties of the object are fetched when you expand the entry in the console, not when you make the call. 当您在控制台中展开条目时(而不是在进行调用时),将获取对象的属性。

Example: 例:

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

When I now click the marker next to Array[0] , I get: 现在,单击Array[0]旁边的标记时,我得到:

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

where one would expect that it does not show 0: 5 as it was added later (after the console.dir call). 其中一个会认为它显示0: 5 ,因为它是后来添加(后console.dir调用)。

You are showing the length property (with console.log ) before something is added to the array, so I suppose 0 is the right length there. 向数组中添加内容之前,您正在显示length属性(带有console.log ),因此我想那里的长度是0。 After that, you are looping an empty array. 之后,您将循环一个空数组。

Furthermore, should sectors have length , then for ( var i=0; i != sectors.length; i++ ) can run endless, because it skips i when it's not sectors.length and moves on. 此外,如果扇区具有length ,则for ( var i=0; i != sectors.length; i++ )可以无休止地运行,因为当它不是ectors.length时它会跳过i并继续前进。 Use i<sectors.length . 使用i<sectors.length

i suppose for this line sectors[sectors.length] = json.area; 我想为此行sectors[sectors.length] = json.area; sectors is out of the scope var sectors = new Array(); 扇区不在范围内var sectors = new Array(); should be in the main scope outside any function 应该在任何功能之外的主要范围内

Shouldn't the following: 以下内容不应该:

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

be

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM