How to create a JavaScript Map

How to create a Map in JavaScript.

I'm going to create a map with following keys and values
"mykey0" -> "myValue0"
"mykey1" -> "myValue1"
"mykey2" -> "myValue2"

map.js

var myMap = {};


//Fill map
for ( var i = 0; i < 3; i++) {
    myMap['mykey' + i] = "myValue" + i;
}

//Get map elements by key
for (key in myMap){
    alert(myMap[key]);
}

No comments:

Post a Comment