如何在javascript中创建这样的数组?

时间:2011-12-08 06:39:39

标签: javascript arrays multidimensional-array

s1=[[[1, 10],[3,5.12],[5,13.1],[7,3],[9,8],[11,2]]]

现在什么是s1?数组 ?二维数组?然后如何创建它?

1 个答案:

答案 0 :(得分:1)

s1是变量,包含数组,包含数组,包含数组:)

我们可以像这样简化您的代码:

s1=[ //this is first Array
    [ // this is first and last element of previous Array, and it is Array too
        [1, 10], // elements of Array, they are Arrays too and contain 2 numbers
        [3,5.12],
        [5,13.1],
        [7,3],
        [9,8],
        [11,2]
    ]
];

如果我们想获得一个数字,例如5,你可以这样做:

console.log(s1[0][2][0]); // 5