1

我有这样的结构:

var Array = new Array(3);

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

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

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

例如,我如何得到“b”

4

4 回答 4

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

于 2012-11-22T17:37:16.867 回答
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"
于 2012-11-22T17:39:54.803 回答
0

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

var obj = {};

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

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

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

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

您的代码正在做的是向 native 添加一堆属性Array(这是一个创建数组对象的函数对象)。有关数组和其他对象之间区别的更多信息,请参阅此问题

于 2012-11-22T17:36:21.013 回答
0

使用这样的东西(你不需要引号):

array[123][1]
于 2012-11-22T17:37:15.473 回答