Robert said:
How do you get the length of an associative array?
Javascript doesn't have 'associative arrays', it has objects to which
arbitrarily named properties can be added at runtime. This is true of
all objects in javascript including Object objects, Arrays and
functions. This behaviour is often called an 'associative array' or a
'hash table' but in reality it is neither, and the use of those terms to
label the behaviour allows individuals with experience of languages that
do have associative arrays and hash tables to have expectations of the
behaviour of javascript that differs from how it actually behaves.
The differences between an object created with new Object() and one
created with new Array are:-
1. The internal [[Prototype]] properties refer to
Object.prototype and Array.prototype respectively.
2. The internal [[Put]] method of the native ECMAScript
object is replaced for the Array with an alternative
that tests its property name argument to see if it is
either "length" or an 'array index', and performs some
additional actions in the event that the property name
used is either. (The standard internal [[Get]] method
is unchanged.
3. A "length" property is added to the object.
In all other respects the objects are identical and exhibit the same
behaviour.
my_cars["cool"]="Mustang";
Javascript has no special array accessing syntax, in the way, for
example, Java does. It has two property accessor notations; dot notation
and bracket notation.
my_cars['cool'] //bracket notation
is identical in behaviour to:-
my_cars.cool //dot notation
- in that they both resolve - my_cars - into a reference to an object
and then attempt to read the value of a property with the name "cool"
from that object.
Dot notation has the limitations that the token following the dot must
be an identifier; conforming to a limited character pattern and fixed at
the point of writing the code. Bracket notation uses an expression
within the brackets, which may be any string literal, but might also be
resolved/evaluated at runtime. Thus the possible property names on
objects are not limited, but the property names used with dot notation
property accessors can o9nly be the sub-set of possible names that also
qualify as Identifiers.
'Array index' property names are string representations of non-negative
integers less than 2 to the power of 32, and these property names can
never conform with the rules that defined Identifiers, so they can only
be referenced using bracket notation property accessors. (Bracket
notation property accessors always type-convert the evaluated result of
the expression within the brackets to a string, so arrayRef[0] is
equivalent to arrayRef["0"], and property names are always strings).
The fact that using a numeric index with an Array can only be done with
a bracket notation property accessor may give the impression that there
is a special array accessing syntax in javascript, because of the
similarities in the resulting code with languages like Java, but that is
not actually the case.
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(my_cars.length);
In the event that a value was assigned to an Array object with an 'array
index' property name (a string representation of a non-negative integers
less than 2 to the power of 32) then the special internal [[Put]] method
of the Array would examine the Array's "length" property and if it was
smaller than or equal to the numeric equivalent of 'array index'
property name it would assign "length" a value that was one greater.
None of the property names used above qualify as an 'array index' so the
Array's special internal [[Put]] method does no more than assign the
value provided to a property of the object with the name specified, in
exactly the same way as it would for any other object.
This reports 0. I know I can write a helper method,
but I would rather not iterate through an array to
find its length:
<snip>
Your problem is conceptual. You are interacting with an Array as if it
was an object (which it is), and objects do not have an interest in the
number of named properties they posses at any time.
Richard.