Associative arrays in Javascript? - Stack Overflow

Currently, I am creating a 3d array in js using the following:var arr = [["name1", "plac

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.

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.

Share Improve this question asked Aug 3, 2010 at 18:26 LegendLegend 117k123 gold badges284 silver badges406 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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"]
};

The situation has changed in the six years since this question was asked.

Due to weak typing associative arrays can be faked in JavaScript:

>> var names = new Array();
undefined

>> names["first"] = "Dotan";
"Dotan"

>> names["last"] = "Cohen";
"Cohen"

>> for ( key in names ) { console.log(key+" "+names[key]) }
undefined
first Dotan
last Cohen

That is sometimes useful, and all browsers released since 2012 support it, but there are caveats! The array cannot be simply read back:

>> names
Array [  ]

More importantly, the array's length cannot be easily retrieved:

>> names.length
0

Therefore this is not an associative array in the sense that JavaScript would have supported it had it been intended, but rather a workaround that is often useful if for whatever reason a real JS object does not support what you need:

>> var names = {};
undefined

>> names.first = "Dotan";
"Dotan"

>> names.last = "Cohen";
"Cohen"

>> for ( key in names ) { console.log(key+" "+names[key]) }
undefined
first Dotan
last Cohen

>> names
Object { first: "Dotan", last: "Cohen" }

>> Object.keys(names).length
2

Javascript is a prototype based dynamic language. You can create objects and change their structure when you want.

var o = {name1: {place1: data1}, name2: {place2: data2}};

and access it with:

o.name1

The look-up implementation varies though and when you have a lot of properties that often changes this can be pretty slow (except in Chrome that uses a special scheme to access object properties i.e. embedded classes a-la-dynamic dispatch from smalltalk). Some libraries (e.g. MooTools) provide some hash map related structures,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742300894a4418009.html

相关推荐

  • Associative arrays in Javascript? - Stack Overflow

    Currently, I am creating a 3d array in js using the following:var arr = [["name1", "plac

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信