0

Possible Duplicate:
Dynamically creating keys in javascript associative array

usually we initialize an array like this:

var ar = ['Hello', 'World'];

And for access to it's values we do:

alert(ar[0]); // Hello

But I want a custom index to assign, ex:

var ar = ['first' => 'Hello', 'second' => 'World'];

and then

alert(ar['first']);

But I can't find how, is there something like this that I could do for assign?

Thank's!

Community
  • 1
  • 1
Neo
  • 395
  • 2
  • 6
  • 23
  • 2
    http://stackoverflow.com/a/351723/1238887 – ahren Dec 05 '12 at 20:34
  • There is no such thing as associative arrays in JavaScript. You either have arrays with numeric indices, or you have objects: unordered collections of keys and values – Bergi Dec 05 '12 at 20:36

6 Answers6

2

You could use Object instead of Array, you can specify named properties for object

var ar = {
  first: 'hello',
  second: 'world'
};
alert(ar['first']);

Also you can just assign properties with string keys to your array, this will also work:

var ar = [];
ar['first'] = 'first';
alert(ar['first']);
Ivan Solntsev
  • 2,081
  • 2
  • 31
  • 40
  • Actually I didn't know I could access to an object's node as a string key, but now I know, Thank's to everyone! – Neo Dec 05 '12 at 20:43
2

You need to use an Object.

var obj = {'first': 'hello', 'second': 'world'};

alert(obj.first);
barro32
  • 2,559
  • 23
  • 36
1

You can do this:

var ar = {
   'first' :'Hello', 
   'second' : 'World'
 };

As you can see, this is the way you initialize objects in Javascript. Javascript blurs the lines between associative arrays and objects.

You can then access this with:

ar['first']

Or even:

ar.first

Also, you could leave out the quotes in the key initialization, like so:

var ar = {
   first :'Hello', 
   second : 'World'
 };
RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23
  • Note that this isn't an array anymore. (But it never was -- proper arrays don't have named indexes.) –  Dec 05 '12 at 20:35
  • Please explain the differences between his array form `['value']` and your object-form `{'key' : 'value'}`. – David Thomas Dec 05 '12 at 20:35
  • @DavidThomas: This is just the way you initialize hashes/objects in javascript. Javascript blurs the lines between the two. You can do ar['first'] at this point or even ar.first. – RonaldBarzell Dec 05 '12 at 20:37
  • Thank's! Yeah I know this is an object, but it works for me as well, I just want to get the indexes through a class parameter for send it's value to a php file, again Thank you very much :) – Neo Dec 05 '12 at 20:40
  • @user1161318, yeah, I know. I was asking you to explain for the OP's benefit... =) – David Thomas Dec 05 '12 at 20:45
  • @DavidThomas: Oops! I will go ahead and edit the answer. Good call. – RonaldBarzell Dec 05 '12 at 20:46
1

Objects in JavaScript are just property bags (hashtables).

You can:

var ar = {};
ar["name"] = "Dave";
ar["salary"] = "Millions";

alert(ar.name);  //Dave
alert(ar["salary"]);  //millions

JavaScript allows you to be pretty flexible in how you create these objects.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
1

JavaScript doesn't have associative arrays as such, but object literals:

var obj = {foo:'bar'};
obj.something = 'else';
//or:
obj['foo'] = 'BAR';

JS won't make a fuss if you create named indexes on an array (because the Array object traces back to the Object prototype) but you'll loose all use of Array features (methods like sort, or the magic length property, to name just a few)

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

just use

    var h = new Object(); // or just {}
h['one'] = 1;
h['two'] = 2;
h['three'] = 3;

// show the values stored
for (var k in h) {
    // use hasOwnProperty to filter out keys from the Object.prototype
    if (h.hasOwnProperty(k)) {
        alert('key is: ' + k + ', value is: ' + h[k]);
    }
}
thehilmisu
  • 342
  • 1
  • 5
  • 13