如何在数组中的数组中获取值

时间:2012-11-22 17:33:32

标签: javascript arrays

我得到了这样的结构:

var Array = new Array(3);

Array["123"] = ["a","b","c"];

Array["456"] = ["d","e","f"];

Array["789"] = ["g","h","i"];

例如,我如何得到" b"

4 个答案:

答案 0 :(得分:1)

var a = new Array();   
a["123"] = ["a","b","c"];
a["456"] = ["d","e","f"];
a["789"] = ["g","h","i"];
b = a["123"][1];

示例:) http://jsbin.com/agolef/1/edit

答案 1 :(得分:1)

a["123"][1]; // yields "b"
a[123][1]; // also yields "b"

使用字符串索引数组可能不是您想要做的。

var a = new Array(3);

a["123"] = ["a","b","c"];  // "123" causes the array to expand to [0..123]
a["123"][1]; // yields "b"
a[123] = ["a","b","c"];  // this has better performance and is idiomatic javascript.
a[123][1]; // also yields "b"
a["456"] = ["d","e","f"];
a["789"] = ["g","h","i"];

如果您想将对象用作地图,请尝试以下方法:

a = new object()
a["123"] = ["a","b","c"];
a["123"][1]; // yields "b"

答案 2 :(得分:0)

Array是本机构造函数。使用不向本机对象添加属性的新对象:

var obj = {};

obj["123"] = ["a","b","c"];

obj["456"] = ["d","e","f"];

obj["789"] = ["g","h","i"];

obj["123"][1]; // "123"

您的代码正在做的是向本机Array添加一堆属性(这是一个生成数组对象的函数对象)。有关数组与其他对象之间差异的更多信息,请参阅this question

答案 3 :(得分:0)

使用类似的东西(你不需要引号):

array[123][1]