有人可以解释这个(奇怪的)行为吗? 为什么第一个例子中的长度为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 0Could 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.. :-)
更多推荐
发布评论