问题描述
有人能解释这个(奇怪)的行为吗?为什么在第一实施例3和不为2,以及最重要的长度,这是为什么在第二示例0的长度?只要密钥数值,长度起作用。如果不是,长度为0。我怎样才能从第二个例子正确的长度?谢谢你。
A = [];
一[1] = {字符串1:串,字符串2:串};
一[2] = {字符串1:串,字符串2:串};
警报(则为a.length); //返回3B = [];
B〔KEY1] = {字符串1:串,字符串2:串};
B〔键2] = {字符串1:串,字符串2:串};
警报(b.length个); //返回0
有一点需要注意的是,有定期的数组和关联数组之间的差异。在常规阵列(真实阵列),该指数已是一个整数。另一方面,关联数组可以使用字符串作为指标。如果你喜欢,你能想到的关联数组作为地图。现在,还要注意,真正的数组总是从零开始。因此,在你的榜样,您通过以下方式创建数组:
A = [];
一[1] = {字符串1:串,字符串2:串};
一[2] = {字符串1:串,字符串2:串}
JavaScript是能够将您的字符串索引转换成数字,因此,您的code以上变为:
A = [];
一[1] = {嗒嗒};
A [2] = {嗒嗒};
但是,请记住我刚才所说:真正的数组从零开始。因此,JavaScript的跨preTER自动分配一个[0],将不确定的。尝试一下在任萤火或铬/ Safari浏览器的控制台,你会看到这样的事情,当您尝试打印A。你应该得到的东西,如[未定义,对象,对象]。因此,大小为3而不是2,你的预期。
在你的第二个例子,我是pretty确保您正在尝试模拟使用关联数组,它本质上是增加属性的对象。记住相关阵列允许您使用字符串作为重点。因此,在其他条件,要添加一个属性的对象。因此,在你的例子:
B [键1] = {字符串1:串,字符串2:串};
这真正含义是:
b.key1 = {字符串1:串,字符串2:串};
初始化B = []简单地创建一个数组,但你的任务不填充数组。它只是给出了B额外的属性。希望这有助于..: - )
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 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.. :-)
这篇关于JavaScript数组长度上的对象数组不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!