views:

414

answers:

3

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
+3  A: 

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

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.

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.

outis
Actually it's the other way around: all keys are strings, so `a[3]` is equivalent to `a["3"]`. If you say `for (i in array)`, `i` will be `"3"` and not `3`. Write `a[1000000000000000000000]= true` and you will have an array with a string key `"1e+21"`, with `length` unaffected.
bobince
@bobince: 1e21 is a different case. Since it's larger than 2**32-1 (too large to be a 32-bit int), it isn't an array index. Equivalence is symmetric, so 'a[3] is equivalent to a["3"]' says the same thing as 'a[3] is equivalent to a["3"]'. My point is that putting array indices in quotes makes no difference as to behavior, not that an element's name is an integer, as it is for other languages. My statement about automatic conversion is misleading; I'll rewrite that.
outis
@bobince: I'm trying to ignore the differences between implementation, model (as specified in the standard) and notion (how everything is to be treated in a programmer's head), which will just complicate matters.
outis
+3  A: 

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

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

This program will help you see that:

a = [];
a["1"] = {};
a["4"] = {};
alert(a.length); // Prints 5
Itay
Thank you for your answer. I guess it's a "feature" :). So, is there a way to get the amount of items in array b without having to count them manually? Or is this the only way:var len = 0;for (var name in b) len++;
Serenti
No, that's the only way. `Object` isn't really a proper associative-array is JavaScript; you can *mostly* use it as if it is, but there are pitfalls.
bobince
Object is not an Array at all. Teh confusion comes because one way to assign a property is via the a[b] syntax...but its no different from saying a.b
plodder
+5  A: 

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

The Code Pimp
+1 For explaining a common misconception. Associative Arrays are not arrays at all, they are just complex objects
plodder
And in JS, Arrays aren't necessarily even arrays in the sense of a contiguous region of memory, where indices are offsets from the start. But those details are unnecessary.
outis