如何在函数中编写所需的数组名称?

时间:2014-09-15 16:49:04

标签: javascript html

我想在输入字段中写入数组的名称(a,b或我选择的其他数组)。如何在函数中编写所需的数组名称?输入[3]。价值?这不行。这是怎么回事?我有4个字段。第四个字段是更好的选择选项。

1 个答案:

答案 0 :(得分:0)

使用数组数组。或者更好的是,一个对象:

var arrays = {
  a : [
    ["Name1",4,"x",5,4,5,4,5],
    ["Name2","x",2,"x",4,4,3,4],
    ["Name3",5,4,"x",3,3,4,4],
    ["Name4",4,2,3,4,"x",3,4],
    ["Name5","x","x",3,4,"x","x","x"]
  ],
  b : [
    ["Name1",4,"x",5,4,5,4,5],
    ["Name2","x",2,"x",4,4,3,4],
    ["Name3",5,4,"x",3,3,4,4],
    ["Name4",4,2,3,4,"x",3,4],
    ["Name5","x","x",3,4,"x","x","x"]
  ]
}

然后您可以像这样使用它们:

arrays.a

或者这个:

arrays['a']

这是第二种形式,允许您传递要使用的数组的名称: