Brett
Brett

Reputation: 2746

Javascript Arrays - Specify Key

Is it possible to create a javascript array similar to PHP's method which allows you to define the keys during creation?

$arr = array("foo" => "bar", 12 => true);

Or can this only be done like:

myArray["foo"] = "bar";
myArray[12] = true;

Upvotes: 1

Views: 1507

Answers (1)

Paolo Bergantino
Paolo Bergantino

Reputation: 488374

In javascript you can achieve this with objects:

var arr = {
    foo: 'bar',
    12: true
};

Upvotes: 2

Related Questions