30

Is there a short way of declaring an associative array like in PHP?

$myArray = array('a' => 'b'); // PHP Way 

In JavaScript I'd do it this way:

var myArray = []; myArray['a'] = 'b'; 

I'm not looking for JavaScript objects.

8
  • "I'm not looking for JavaScript objects." Why not? Is there a particular reason you want non numeric properties on an Array? Commented Jul 31, 2011 at 15:28
  • A friend asked about a short way and I didn't find any. I just want to know wether there is a short way or not. Commented Jul 31, 2011 at 15:31
  • Well if you really want an Array, and not a plain Object, then no, there isn't anything aside from creating a helper function and passing it an Object to populate the Array Commented Jul 31, 2011 at 15:33
  • @dotweb: Have fun avoiding objects when working with JavaScript. :) Commented Jul 31, 2011 at 15:33
  • This has been cleared up time and again. Just look at the related posts on the right. Commented Jul 31, 2011 at 15:40

4 Answers 4

33

Declare an object like this:

var myArray = {"a": "b", "c": "d"}; 

... and then refer to each item like this:

var somethingElse = myArray["a"]; //Sets "somethingElse" to "b". 

As @Chris and @Marc mention in the comments: in JavaScript, objects ARE associative arrays, and vice versa, they just refer to two different ways of doing the same thing. For Example...

var somethingElse = myArray["a"]; var anotherVariable = myArray.a; 

... do the same thing.

Sign up to request clarification or add additional context in comments.

3 Comments

You should mention there is no such thing as associative arrays in javascript.
Wrong. In javascript, all arrays are associative arrays.
@Chris - As far as I was aware, associative arrays and objects are basically the same thing in JavaScript. They're just different ways of referring to the same thing. e.g. myArray["a"] vs myArray.a
31

JavaScript does not have associative arrays. In your example, you declare myArray as array but then you assign an object to it. So your code is not different from this:

var myObject = {}; myObject['a'] = 'b'; 

Update: A little correction. Actually, the original code does not destroy the array. It just appends a regular property to it. That's possible because JavaScript arrays are a subset of JavaScript objects.

4 Comments

which is the same as myObject.a = 'b';, since you can use dot notation as long as the property name is a valid JS variable name, so you could not use dot notation for myObject['no dot!'] = 'b'.
Nonsense. Of course javascript has associative arrays. For all intents and purposes thats what a Javascript Object IS. How it works under the hood is neither here nor there.
@RichardRiley - A JavaScript object is an unordered collection of properties. The concept of order is possibly the key point of arrays.
This is not 100% correctly, and the two are not the same. You cannot iterate, in order, on an Object since it does not work like an Array, which was designed to be iterate-able. The question is how to create such an array in a non-repeatable manner.
9

I'm not looking for JavaScript objects.

There are no "associative arrays" in JavaScript, just objects comprised of property names and values which can be treated as such. So, what you are looking for is in fact objects. In fact, this example from your question is working with an object:

var myArray = []; myArray['a'] = 'b'; alert(typeof myArray); // 'object' 

You initially construct the object using the array literal syntax:

var myArray = []; 

which means you have created an object which inherits from Array.prototype. So essentially, you are dealing with an object which has been augmented with the methods stored in Array.prototype.

To check that myArray is actually an array (well, I would say JavaScript arrays are pseudo-arrays), you can check its constructor:

alert(typeof myArray === 'object' && myArray.constructor === Array); 

There's a better way which will also identify arrays constructed in different windows and frames:

alert(Object.prototype.toString.apply(myArray) === '[object Array]'); 

You can also use instanceOf:

alert(myArray instanceof Array); 

or Array.isArray:

alert(Array.isArray(myArray)); 

1 Comment

"The only way to tell..." You're forgetting about [] instanceof Array, Object.prototype.toString.call( [] ) and Array.isArray( [] ). ;o)
6

In javascript, associative arrays, keyed collections, hashes, ... whatever you want to call them, are not a special type. All the following are good.

a = {} a[3] = 15 a.b = "c" a['def'] = 'something' 

This code produces a single valid object with the properties you would expect. All of them. You can combine a conventionally indexed array and an associative array in one object.

As to declaring a whole bunch at once, the usual syntax is:

a = { 'key1' : 'val1', 'key2' : val2, key3 : val3, key4 : "val4" } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.