bogdan
bogdan

Reputation: 1289

variable as index in an associative array - Javascript

I'm trying to create an associative array, create an empty array, and then add a (indexName -> value) pair:

var arrayName = new Array;

arrayName["indexName"] = value;

// i know i can also do the last line like this:

arrayName.indexName = value;

When I assign the value to the indexName I want indexName to be dynamic and the value of a variable. So I tried this:

arrayName[eval("nume")] = value;

Where:

var var1 = "index";
var var2 = "Name";

var nume = '"' + var1 + var2 + '"'; 

but: alert(arrayName["indexName"]); doesn't return "value"... it says "undefined"

Is there something I’m missing? (I’m not familiar with eval() ); if the way I’m trying is a dead end, is there another way to make the index name of the associative array value dynamic?

Upvotes: 30

Views: 89019

Answers (7)

Mahib
Mahib

Reputation: 4063

I did something like like following;

let parentArray = [];
let childArray = [1, 2, 3];
childArray.name = 'my-array-1';
parentArray.push(childArray);

To access that by name in ES6;

parentArray.filter(x => x.name == 'my-array-1');

Upvotes: 0

bogdan
bogdan

Reputation: 1289

Indeed, there was no need for an array object, a simple object did the job; further more an array introduced the need to use quotes inside the square brackets obj["var1 + var2"] to access the object property's value and not the value associated with an index (i think); the quotes transformed "var1 + var2" into a string. Using a simple object eliminated the need for quotes, so I can use obj[var1 + var2], wich worked :)

Thanks everyone !

Upvotes: 0

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

use arrayName[var1+var2]

Note that arrayName.var is the same as arrayName["var"] -- it's just syntactic sugar. The second form is mostly used when dealing with the kind of problems that you are facing - dynamic object keys, and keys that are not alphanumeric (think of arrayName[".eval()"]; this is a perfectly legal object key that has nothing to do with the javascript eval() function)

Upvotes: 5

balu
balu

Reputation: 3689

You would use objects to do that:

var hash = {}
hash["foo"] = "foo";
hash.bar    = "bar";
// This is the dynamic approach: Your key is a string:
baz         = "baz"
hash[baz]   = "hello";

To iterate, just use a for loop or $.each() in jQuery.

Upvotes: 7

Nick Craver
Nick Craver

Reputation: 630389

You want a plain object with the same bracket notaiton here, like this:

var arrayName = {};
arrayName["indexName"] = value;

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827316

At first I don't think you need a real array object to do what you need, you can use a plain object.

You can simply use the bracket notation to access a property, using the value of a variable:

var obj = {};
var nume = var1 + var2;
obj[nume] = value;

Array's are simply objects, the concept of an "associative array" can be achieved by using a simple object, objects are collections of properties that contain values.

True arrays are useful when you need to store numeric indexes, they automatically update their length property when you assign an index or you push a value to it.

Upvotes: 28

meder omuraliev
meder omuraliev

Reputation: 186562

Are you looking for variableName = 'bla'+'foo'; arrayRef[variableName] = 'something'; ?

And even so, you should use an object literal instead. x = {}; x[variablename] = 'blah';

Upvotes: 0

Related Questions