如何通过javascript对象文字的变量值分配多个键

时间:2012-06-01 15:32:41

标签: javascript

我想知道是否有可能有一个像多维数组的对象? 我想要的是:

lines[first][first first]= "value key key"        


    lines          =       {};
    key            =       "first";
    key_key        =       "first first";
    lines[key]     =       "value key ";
    lines[key][key_key]=   "value key key "

    console.log(lines);

输出: 无法将undefined转换为对象

1 个答案:

答案 0 :(得分:1)

lines[key]是一个字符串值。它应该是一个对象。像这样:

lines              = {};
key                = "first";
key_key            = "first first";
lines[key]         = {};
lines[key][key_key]= "value key key ";
//=> lines.first['first first'] now is 'value key key'

lines              = {};
key                = "first";
key_key            = "first first";
lines[key]         = new String("value key ");
lines[key][key_key]= "value key key ";
//=> lines.first['first first'] still is 'value key key', 
//   but now it's a custom property of a String Object