Javascript 对象数组上的Javascript数组长度不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:46:08  来源:igfitidea点击:

Javascript array length incorrect on array of objects

javascriptarrays

提问by Serenti

Could someone explain this (strange) behavior? Why is the length in the first example 3 and not 2, and most importantly, why is the length in the second example 0? As long as the keys are numerical, length works. When they are not, length is 0. How can I get the correct length from the second example? Thank you.

有人可以解释这种(奇怪的)行为吗?为什么第一个例子中的长度是 3 而不是 2,最重要的是,为什么第二个例子中的长度是 0?只要键是数字的,长度就有效。如果不是,则长度为 0。如何从第二个示例中获得正确的长度?谢谢你。

a = [];
a["1"] = {"string1":"string","string2":"string"};
a["2"] = {"string1":"string","string2":"string"};
alert(a.length); // returns 3

b = [];
b["key1"] = {"string1":"string","string2":"string"};
b["key2"] = {"string1":"string","string2":"string"};
alert(b.length); // returns 0

回答by The Code Pimp

One thing to note is that there is a difference between regular arrays and associative arrays. In regular arrays (real arrays), the index has to be an integer. On the other hand, associative arrays can use strings as an index. You can think of associative arrays as a map if you like. Now, also note, true arrays always start from zero. Thus in your example, you created an array in the following manner:

需要注意的一件事是常规数组和关联数组之间存在差异。在常规数组(真实数组)中,索引必须是整数。另一方面,关联数组可以使用字符串作为索引。如果您愿意,您可以将关联数组视为地图。现在还要注意,真正的数组总是从零开始。因此,在您的示例中,您按以下方式创建了一个数组:

a = [];
a["1"] = {"string1":"string","string2":"string"};
a["2"] = {"string1":"string","string2":"string"}

Javascript was able to convert your string indexes into numbers, hence, your code above becomes:

Javascript 能够将您的字符串索引转换为数字,因此,您上面的代码变为:

a = [];
a[1] = {"blah"};
a[2] = {"blah"};

But remember what i said earlier: True arrays start from zero. Therefore, the javascript interpreter automatically assigned a[0] to the undefined. Try it out in either firebug or the chrome/safari console, and you will see something like this when you try to print "a". You should get something like "[undefined, Object, Object]. Hence the size 3 not 2 as you expected.

但请记住我之前说过的话:真正的数组从零开始。因此,javascript 解释器会自动将 a[0] 分配给 undefined。在 firebug 或 chrome/safari 控制台中尝试一下,当您尝试打印“a”时,您会看到类似的内容。您应该得到类似“[undefined, Object, Object] 的信息。因此,大小为 3,而不是您预期的 2。

In your second example, i am pretty sure you are trying to simulate the use of an associated array, which essentially is adding properties to an object. Remember associated arrays enable you to use strings as a key. So in other terms, you are adding a property to the object. So in your example:

在您的第二个示例中,我很确定您正在尝试模拟关联数组的使用,这实质上是向对象添加属性。记住关联数组使您能够使用字符串作为键。因此,换句话说,您正在向对象添加一个属性。所以在你的例子中:

b["key1"] = {"string1":"string","string2":"string"};

this really means:

这真的意味着:

b.key1 = {"string1":"string","string2":"string"};

Initializing b =[] simply creates an array, but your assignment doesn't populate the array. It simply gives "b" extra properties. Hope this helps.. :-)

初始化 b =[] 只是创建一个数组,但您的赋值不会填充该数组。它只是给“b”额外的属性。希望这可以帮助.. :-)

回答by Itay Maman

length returns 1 + the largest integer key in the object.

length 返回 1 + 对象中最大的整数键。

In athe largest key is 2 so 1+2 is 3.

a最大关键是2所以1 + 2是3。

In bthere are no integer keys (the keys there are key1and key2which cannot be converted into ints) so Javascript assumes that the largest key is -1, and 1 + -1yields 0.

b没有整数键(键有key1key2不能被转换成整数)这样的Javascript假定最大关键是-1,和1 + -1产量0

This program will help you see that:

该程序将帮助您了解:

a = [];
a["1"] = {};
a["4"] = {};
alert(a.length); // Prints 5

回答by outis

From the ECMAScript standard, ECMA-262, 5th ed.

来自 ECMAScript 标准,ECMA-262,第 5 版。

15.4.5.2 length

The length property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.

15.4.5.2 长度

此 Array 对象的 length 属性是一个数据属性,其值在数字上始终大于名称为数组索引的每个可删除属性的名称。

Note the length property of an array only takes into account array indices, which are integers; setting other properties doesn't affect length.

注意数组的长度属性只考虑数组索引,它们是整数;设置其他属性不会影响长度。

For an array, a["3"]is equivalent to a[3](this behavior is specified by § 15.4.5.1); 3 is an array index rather than a property. Thus setting a["3"]affects the array's length. b["key1"]is equivalent to b.key1. Setting properties don't affect the length of a collection.

对于数组,a["3"]等效于a[3](此行为由第 15.4.5.1 节指定);3 是数组索引而不是属性。因此设置a["3"]会影响数组的长度。b["key1"]相当于b.key1。设置属性不会影响集合的长度。