0

我正在尝试调用数组中的元素,即在另一个数组中,即在另一个数组中。例如....

  var a1 = ["1","2","3"];
  var a2 = ["4","5","6"];
  var a3 = ["7","8","9"];
  var a4 = ["10","11","12"];

  var b1 = ["a1","a2"];
  var b2 = ["a3","a4"];

  var c = ["b1","b2"];

  var x = c[0];

  console.log(x);

答案是x = b1

我怎样才能定义var x = c[0] b1[1] a2[2]答案是6?

4

4 回答 4

0

将数组定义为其他数组的元素,而不是字符串:

var a1 = [1, 2, 3];
...
var b1 = [a1, a2];
...
var c = [b1, b2];

var x = c[0][0][0]; // 1
于 2012-10-04T21:57:54.790 回答
0

数组a1实际上并不在b1其中——只有一个包含文本“a1”的字符串。如果你这样定义它们:

var a1 = ["1","2","3"];
var a2 = ["4","5","6"];
var a3 = ["7","8","9"];
var a4 = ["10","11","12"];

var b1 = [a1, a2];
var b2 = [a3, a4];

var c = [b1, b2];

然后你可以通过写得到“6” c[0][1][2]

于 2012-10-04T21:58:36.307 回答
0

你想要的是嵌套数组。

var a1 = ["1","2","3"];
var a2 = ["4","5","6"];
var a3 = ["7","8","9"];
var a4 = ["10","11","12"];

var b1 = [a1,a2];
var b2 = [a3,a4];

var c = [b1,b2];

var x = c[0][1][2];

console.log(x);
于 2012-10-04T21:58:37.887 回答
0
var a1 = ["1","2","3"], a2 = ["4","5","6"], a3 = ["7","8","9"], a4 = ["10","11","12"];
var b1 = [a1,a2];
var b2 = [a3,a4];
var c = [b1,b2];

var x1 = c[0][0][0];
var x2 = c[0][0][1];
var x3 = c[0][0][2];

console.log(x1); //1 (1st/0 item of a1) from c
console.log(x2); //2 (2nd/1 item of a1) from c
console.log(x3); //3 (3rd/2 item of a1) from c

演示

于 2012-10-04T22:00:53.050 回答