オブジェクトのプロパティは任意の文字列にすることができます
var o = {
"whatever you want":1,
"1":"numbers too"
}
o[2]="no numbers, convert to string"
console.log(o["whatever you want"]);//=1
console.log(o["1"]);//=numbers too
console.log(o[new String(2)]);//=no numbers, convert to string
// as James commented, you don't have to convert to string
// I usually do that as to not confuse the object with an array
console.log(o[2]);//=no numbers, convert to string
すべての「プロパティ名」が正の整数または 0 の場合、配列を使用できます。
var arr=new Array();//or var arr=[];
var b=22;
arr[10000]="hi";
arr[b]="value";
// normally you would do for(var i=0;len=arr.length;i<len;i++){...arr[i]
// but because there may be many empty spots it's quicker to do:
for(thing in arr){
if(arr.hasOwnProperty(thing)){
console.log(thing + " is " + arr[thing]);
}
}