热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

对象数组的Javascript数组长度不正确-Javascriptarraylengthincorrectonarrayofobjects

Couldsomeoneexplainthis(strange)behavior?Whyisthelengthinthefirstexample3andnot2,a

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

3 个解决方案

#1


84  

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:

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

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.

但是记住我之前说过的:真正的数组从0开始。因此,Javascript解释器自动将[0]分配给未定义的。在firebug或chrome/safari控制台中尝试一下,当您尝试打印“a”时,您将看到类似的内容。您应该得到“[未定义的、对象、对象]”。因此尺寸是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额外的属性。希望这可以帮助. .:-)

#2


16  

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

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

In a the largest key is 2 so 1+2 is 3.

a的最大键是2 1+2 = 3。

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

在b中没有整数键(键1和key2不能转换为ints),所以Javascript假设最大的键是-1,而1 + -1的收益率为0。

This program will help you see that:

这个节目将帮助你看到:

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

#3


9  

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

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

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.

这个数组对象的length属性是一个数据属性,其值总是在数值上大于每个可deletable属性的名称,该属性的名称是一个数组索引。

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.

为一个数组,[3]相当于[3](这种行为规定§15.4.5.1);3是数组索引而不是属性。因此,设置["3"]会影响数组的长度。b[“key1”]等价于b.key1。设置属性不会影响集合的长度。


推荐阅读
author-avatar
beng83790si
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有