Alex Rivera | Logout

How to create dynamic Map key in javascript?

Asked 2011-03-28T07:32:44.743
12

I have tried to create a map like example below...

var myMap= {"one": 1,"two": "two","three": 3.0};

So, I iterate them like:

for (var key in myMap) {

window.alert("myMapmap property \"" + key + "\" = " + myMap[key]);

}

If my data is dynamic... and I want to add each of them in to map... how the codes should be like? And I expect the key is not static...I mean the data taken from other source like database, that wasn't define before..

Thanks before

Edit
Report

1 Answer

8

Dynamic values and keys can be added using this notation:

var myMap = {}; // Create empty map
myMap["some" + "dynamic" + "key"] = "some" + "dynamic" + "variable";
answered 2011-03-28T07:36:48.893

Your Answer