By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,667 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,667 IT Pros & Developers. It's quick & easy.

length of an associative array

P: n/a
Hi All!

How do you get the length of an associative array?

var my_cars= new Array()
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(my_cars.length);

This reports 0. I know I can write a helper method, but I would rather not
iterate through an array to find its length:

function associativeArrayLength(array)
{
length = 0;
for (var object in array) {
length++;
}
return length;
}

Has anyone encountered this before?

Rob
:)
Aug 7 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lee
Robert Mark Bram said:

Hi All!

How do you get the length of an associative array?

var my_cars= new Array()
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(my_cars.length);

This reports 0. I know I can write a helper method, but I would rather not
iterate through an array to find its length:

function associativeArrayLength(array)
{
length = 0;
for (var object in array) {
length++;
}
return length;
}

Has anyone encountered this before?


You're not really adding elements to an array.
Your adding new attributes to the Array object.
It would work just exactly the same as:

var my_cars = new Object();
my_cars["cool"]="Mustang";

Objects (Array or otherwise), have no attribute or method
that reflects the number of attributes they have.

Aug 7 '05 #2

P: n/a
VK
> You're not really adding elements to an array.
Your adding new attributes to the Array object.
It would work just exactly the same as:

var my_cars = new Object();
my_cars["cool"]="Mustang";

Objects (Array or otherwise), have no attribute or method
that reflects the number of attributes they have.


Before saying such nonsense you better learn what is the array, what is
the associative array and how is it reflected in the Object structure.

To OP:
read <http://www.geocities.com/schools_ring/ArrayAndHash.html>

Aug 7 '05 #3

P: n/a
"VK" <sc**********@yahoo.com> writes:
You're not really adding elements to an array.
Your adding new attributes to the Array object.
It would work just exactly the same as:

var my_cars = new Object();
my_cars["cool"]="Mustang";

Objects (Array or otherwise), have no attribute or method
that reflects the number of attributes they have.


Before saying such nonsense you better learn what is the array, what is
the associative array and how is it reflected in the Object structure.


That "nonsense" is absolutely corrent, in all respects, so you might
want to reconsider your reply.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 7 '05 #4

P: n/a
VK said the following on 8/7/2005 5:01 AM:
You're not really adding elements to an array.
Your adding new attributes to the Array object.
It would work just exactly the same as:

var my_cars = new Object();
my_cars["cool"]="Mustang";

Objects (Array or otherwise), have no attribute or method
that reflects the number of attributes they have.

Before saying such nonsense you better learn what is the array, what is
the associative array and how is it reflected in the Object structure.


Sounds like it is *you* who needs to learn better.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 7 '05 #5

P: n/a
VK

Lasse Reichstein Nielsen wrote:
"VK" <sc**********@yahoo.com> writes:
You're not really adding elements to an array.
Your adding new attributes to the Array object.
It would work just exactly the same as:

var my_cars = new Object();
my_cars["cool"]="Mustang";

Objects (Array or otherwise), have no attribute or method
that reflects the number of attributes they have.


Before saying such nonsense you better learn what is the array, what is
the associative array and how is it reflected in the Object structure.


That "nonsense" is absolutely corrent, in all respects, so you might
want to reconsider your reply.


OK, reconsidered. Just was thinking of another attack on my beloved
length :-)

To OP:
you may use Dictionary object on IE:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjdictionary.asp>

And it's rather easy to cross-emulate it for wannabes.

Time for the cuctom made PGH (Pretty Good Hash) #2?

Aug 7 '05 #6

P: n/a
Robert Mark Bram wrote:
Hi All!

How do you get the length of an associative array?


Just a thought, but in addition to Lee's reply, you could prototype a size()
function, which returns the correct length of the array:

Array.prototype.size = function () {
var l = this.length ? --this.length : -1;
for (var k in this) {
l++;
}
return l;
}

var array = new Array();
array['foobar'] = 'bar';
alert(array.size()); // alerts 1
JW


Aug 7 '05 #7

P: n/a
Robert Mark Bram wrote:
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.
var my_cars= new Array()
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.
Aug 7 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.