29

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.

user113716
  • 299,514
  • 60
  • 431
  • 433
js-coder
  • 7,488
  • 8
  • 38
  • 59

4 Answers4

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.

Álvaro González
  • 128,942
  • 37
  • 233
  • 325
  • 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'`. – Peter Ajtai Jul 31 '11 at 16:57
  • 8
    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. – RichieHH Aug 20 '13 at 14:33
  • 2
    @RichardRiley - A JavaScript object is [an unordered collection of properties](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). The concept of *order* is possibly the key point of arrays. – Álvaro González Aug 20 '13 at 18:53
  • 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. – vsync Aug 03 '15 at 15:30
30

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.

Karl Nicoll
  • 14,491
  • 3
  • 46
  • 60
  • You should mention there is no such thing as associative arrays in javascript. – Chris Jul 31 '11 at 15:30
  • Wrong. In javascript, all arrays are associative arrays. – Marc Thibault Jul 31 '11 at 15:33
  • @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` – Karl Nicoll Jul 31 '11 at 15:33
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));
vsync
  • 87,559
  • 45
  • 247
  • 317
karim79
  • 326,960
  • 63
  • 404
  • 402
  • *"The only way to tell..."* You're forgetting about `[] instanceof Array`, `Object.prototype.toString.call( [] )` and `Array.isArray( [] )`. ;o) – user113716 Jul 31 '11 at 15:44
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"
}
Marc Thibault
  • 1,598
  • 1
  • 12
  • 13