0

我试图从数组中的这些数组中获取数字以在新行上打印,但不知道如何引用它们以便将它们打印到控制台。我在这里想念什么?

var numbers = [
    [1,2,3,4,5,6,7],
    [8,9,10,11,12,13,14,15,16],
    [17,18,19],
    [20],
    [21,22,23,24,25,26],
    [27,28,29,30]
];

for (i=0; i<numbers.length; i++) {
    for (j=0; j<numbers[i].length; j++) {
        console.log(numbers[i].j); //The problem is with this j here I suspect...
    }
}
4

4 回答 4

0

尝试

for (i=0; i<numbers.length; i++) {
    for (j=0; j<numbers[i].length; j++) {
        console.log(numbers[i][j]); //The problem is with this j here I suspect...
    }
}
于 2013-08-23T11:57:49.170 回答
0

numbers[i].j 访问 numbers 中每个数组的属性 j,您需要 numbers[i][j] 访问数组 numbers[i] 中的元素 j

于 2013-08-23T11:57:53.607 回答
0

它应该这样读:

var numbers = [
    [1,2,3,4,5,6,7],
    [8,9,10,11,12,13,14,15,16],
    [17,18,19],
    [20],
    [21,22,23,24,25,26],
    [27,28,29,30]
];

for (i=0; i<numbers.length; i++) {
    for (j=0; j<numbers[i].length; j++) {
        console.log(numbers[i][j]);
    }

}
于 2013-08-23T11:58:58.803 回答
0

改变这个

console.log(numbers[i][j]);
于 2013-08-23T12:01:22.860 回答