具有自定义变量索引的Javascript数组

Ala*_*n A 5 javascript arrays variables

是否可以使用变量设置数组的自定义索引.

例如:

var indexID = 5;
var temp = {
    indexID: new Array()
};
Run Code Online (Sandbox Code Playgroud)

上面的示例将数组索引设置为indexID和不5.我尝试使用=snd引号,但我没有任何成功.

Thnaks

Vis*_*ioN 14

是的,只需使用方括号表示法:

var temp = {};
temp[indexID] = [];
Run Code Online (Sandbox Code Playgroud)

还要注意temp作为对象而不是数组的事实.在JavaScript中,所有关联数组(或字典)都表示为对象.

更多: http ://www.jibbering.com/faq/faq_notes/square_brackets.html#vId


小智 5

我想你想要的是:

var indexID = 5;
var temp = {};
temp[indexID] = "stuff"
Run Code Online (Sandbox Code Playgroud)