| Refresh | Home EGTry.com

use Object as a simple map or associated array


commentsJavascript sourceExecution result in your current browser
use a generic object as map
var map={"a":1, "b":2, "c":3};

set value
map["id"]=123;

get value
document.write(map["id"]);

test if it contains a key
document.write(map.hasOwnProperty("a")+"<br/>");
document.write(map.hasOwnProperty("Other"));

loop through map key-value pairs
for(var key in map) {
 document.write(key+" => "+map[key]+"<br/>");
}

loop through all values
for each(var val in map) {
 document.write(val+"<br/>");
}