2016-02-29 74 views
-4

我想在javascript中生成一个二维数组,代表下面的网格。尝试了不同的东西但徒劳无功。 enter image description here使用Javascript生成二维数组

+2

那你试试 – Ramanlfc

+0

所以嵌套在一个阵列'VAR ARR内的阵列= [[1,2,3],[4,5,6],[7,8,9]];'你有什么问题? – epascarello

+2

[如何在JavaScript中创建二维数组?](http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – ManoDestra

回答

0

基本上,你的矩阵中的每一行是前一行向左偏移:

var source = [0,9,4,6,8,2,7,1,3,5] 
 

 
matrix = source.map(function(_, index) { 
 
    return source.slice(index).concat(source.slice(0, index)) 
 
}); 
 

 
matrix.map(row => document.write(row + "<br>"));