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!

2
  • 2
    stackoverflow.com/a/351723/1238887 Commented Dec 5, 2012 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 Commented Dec 5, 2012 at 20:36

6 Answers 6

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']);
Sign up to request clarification or add additional context in comments.

1 Comment

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!
2

You need to use an Object.

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

alert(obj.first);

Comments

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.

Comments

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)

Comments

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]);
    }
}

Comments

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'
 };

6 Comments

Note that this isn't an array anymore. (But it never was -- proper arrays don't have named indexes.)
Please explain the differences between his array form ['value'] and your object-form {'key' : 'value'}.
@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.
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 :)
@user1161318, yeah, I know. I was asking you to explain for the OP's benefit... =)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.