对象数组上的Javascript数组长度不正确 [英] Javascript array length incorrect on array of objects

查看:103
本文介绍了对象数组上的Javascript数组长度不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

推荐答案

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

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 能够将您的字符串索引转换为数字,因此,您上面的代码变为:

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

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

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

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.

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

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"};

这实际上意味着:

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

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

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

这篇关于对象数组上的Javascript数组长度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆