2013-08-23 73 views
0

我想從數組中的這些數組中獲取數字以在新行上打印,但無法弄清楚如何引用它們以便將它們打印到控制檯。我在這裏錯過了什麼?如何使用嵌套For循環在新行上打印數組中的數組值?

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

console.log(numbers [i] [j] – mplungjan

+0

非常感謝您幫助我解決這個問題。 –

回答

0

號碼[I] .J訪問屬性j表示在號碼每個陣列,要數[i] [j],其訪問單元j的數組編號,[I ]

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

} 
0

改變這個

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