Tip Details Add a Related Tip

Rate as: Positive Negative

Length of associative arrays in Javascript

Javascript does not have built-in support for associative arrays, only indexed arrays. Yet in Javascript you can quite easily create associative arrays. So what gives? The answer is that in Javascript when you are creating an associative array, you are really create an object and each entry is a member variable in the object. For example:

var tempArray            = new Array();
tempArray['firstKey']    = 'firstValue;
tempArray['secondKey']   = 'secondValue';
... etc

This will create what looks like an associative array in that you can ask for a value like tempArray['firstKey'] and get back the expected value.

The one big down-side to using and creating associative arrays in this manner is that when you do, none of the "usual" array methods like length, pop, push, etc will work on your array since it's not really an array, it's an object.

So if you are going to use associative arrays in Javascript you'll probably need to create your own functions to replicate these. It's not too hard since the for loops work on these pseudo-arrays. For example, here's a function that will return the length of an associative array in javascript:

function getAssocArrayLength(tempArray) {
   var result = 0;
   for ( tempValue in tempArray ) {
      result++;
   }
	
   return result;
}
Rating: 100% positive, 4 total Votes
Categories: Javascript programming web
Added: on Nov 04, 2008 at 10:13 am
Added By: an anonymous user
Searches: array javascript associ web create

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Static variables in Javascript

93% positive, 27 comments - last added on Oct 26, 2009 at 3:00 am
– Tip added by an anonymous user on Aug 03, 2007 at 4:19 pm

Scrolling HTML elements using the keyboard

80% positive, 1 comment - last added on Jul 18, 2008 at 3:53 pm
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

Detecting if a variable exists in Javascript

100% positive, 2 comments - last added on Oct 21, 2009 at 2:29 pm
– Tip added by an anonymous user on Jul 22, 2008 at 8:31 am

Telling when an iframe is done loading

85% positive, 3 comments - last added on May 16, 2009 at 7:48 am
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Efficient building of large strings in Javascript

100% positive, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

reading select element values in javascript in IE7

100% positive, 0 comments
– Tip added by an anonymous user on Jul 17, 2009 at 1:28 pm

Optional parameters in javascript functions

100% positive, 0 comments
– Tip added by an anonymous user on Jul 30, 2008 at 10:44 am

Javascript variable naming

100% positive, 1 comment - last added on Jul 22, 2008 at 8:33 am
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

Checking for undefined variables in Javascript

100% positive, 1 comment - last added on Jul 14, 2009 at 5:13 pm
– Tip added by an anonymous user on Jan 14, 2009 at 9:47 am

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Code for creating Javascript CSS charts

100% positive, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Creating vector graphics in javascript

100% positive, 0 comments
– Tip added by Marcos84 on May 10, 2007 at 2:44 pm

attaching events to nodes in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 11:23 am

animating elements with jQuery

0% positive, 0 comments
– Tip added by an anonymous user on Aug 04, 2008 at 1:46 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am

Nice article on javascript techniques

no ratings, 0 comments
– Tip added by rlansky on Apr 24, 2007 at 8:12 am

Getting pages to render correctly in IE8

Categories: IE8 web css javascript HTML
100% positive, 0 comments
– Tip added by an anonymous user on Jul 23, 2009 at 10:13 pm

Getting answers from Experts Exchange

Categories: programming web
100% positive, 0 comments
– Tip added by an anonymous user on Apr 14, 2009 at 10:57 am

Don't include a "length" attribute in a JSON Object

100% positive, 0 comments
– Tip added by an anonymous user on Apr 10, 2009 at 4:51 pm

How to impress the judges of 2009 Calling All Innovators Contest

no ratings, 0 comments
– Tip added by alvinmood on Jun 12, 2009 at 6:05 am