繁体   English   中英

如何使用嵌套的For循环在换行上打印数组中的数组值?

[英]How do I use nested For loops to print array values within an array on new lines?

我正在尝试从数组中的这些数组中获取数字以在新行上打印,但无法弄清楚如何引用它们以将其打印到控制台。 我在这里想念什么?

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...
    }
}

尝试

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...
    }
}

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

它应该这样阅读:

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]);
    }

}

改变这个

console.log(numbers[i][j]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM