Javascript数组长度在对象数组上不正确(Javascript array length incorrect on array of objects)

编程入门 行业动态 更新时间:2024-10-26 06:29:58
Javascript数组长度在对象数组上不正确(Javascript array length incorrect on array of objects)

有人可以解释这个(奇怪的)行为吗? 为什么第一个例子中的长度为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

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

最满意答案

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

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

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

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

但请记住我之前所说的:真数组从零开始。 因此,JavaScript解释器会自动为未定义的分配一个[0]。 尝试在firebug或chrome / safari控制台,当您尝试打印“a”时,您会看到类似的东西。 你应该得到像“[undefined,Object,Object”这样的东西,所以你的预期大小不是2。

在第二个例子中,我很确定你正在尝试模拟一个关联的数组的使用,这本质上是向对象添加属性。 记住关联数组可以使用字符串作为关键字。 所以在其他方面,您正在向对象添加一个属性。 所以在你的例子中:

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

这真的意味着:

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

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

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:

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.

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.. :-)

更多推荐

本文发布于:2023-07-20 20:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   上不   长度   对象   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!