Legend Legend - 1 month ago 7
Javascript Question

Associative arrays in Javascript?

Currently, I am creating a 3d array in js using the following:

var arr = [["name1", "place1", "data1"],
["name2", "place2", "data2"],
["name3", "place3", "data3"]];


I can access each element using arr[0] or arr[1]. But is there anyways I can access them using a key like this: arr["name1"] should give me the first one. Any suggestions? I think I am looking for a Hashmap like functionality.

Answer

The only way you could do that is by wrapping it in an object.

var arr = {
    name1 : ["name1", "place1", "data1"],
    name2 : ["name2", "place2", "data2"],
    name3 : ["name3", "place3", "data3"]
};
Comments