具有自定义变量索引的Javascript数组 [英] Javascript array with custom variable indexing

查看:61
本文介绍了具有自定义变量索引的Javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to set the custom index of an array with a variable.

例如:

var indexID = 5;
var temp = {
    indexID: new Array()
};

上面的例子将数组索引设置为 indexID 而不是 5 。我尝试使用 = snd报价,但我没有任何成功。

The above example sets the array index to indexID and not 5. I have tried using = snd quotes but I without any success.

Thnaks

推荐答案

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

Yes, just use square brackets notation:

var temp = {};
temp[indexID] = [];

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

Also pay attention to the fact that temp is an object, and not an array. In JavaScript all associative arrays (or dictionaries) are represented as objects.

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

这篇关于具有自定义变量索引的Javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆