我是javascript的新手,我无法弄清楚如何将二维阵列的元素复制到另一个二维阵列中 这些是我到目前为止所做的3x3阵列
// first 2 dimensional 3 X 3 array
var myArray1 = new Array(3)
for (i=0; i < 3; i++)
myArray1[i]=new Array(3)
var start = 1;
for (var i = 0; i < 3; i++)
{ for (var j = 0; j < 3; j++) { myArray1[i][j] = start; start = start + 1; }
}
for (var i = 0; i < 3; i++)
{ for (var j = 0; j < 3; j++) { document.write(myArray1[i][j] + " ");}
document.write("<br/>");
}
document.write("<br/>");
document.write(" +");
document.write("<br/>");
document.write("<br/>");
// second 2 dimensional 3 X 3 array
var myArray2 = new Array(3)
for (i = 0; i < 3; i++) myArray2[i] = new Array(3)
for (var i = 0; i < 3; i++)
{ for (var j = 0; j < 3; j++) { start = start - 1; myArray2[i][j] = start; }
}
for (var i = 0; i <3; i++)
{ for (var j = 0; j < 3; j++) { document.write(myArray2[i][j] + " "); }
document.write("<br/>");
}
document.write("<br/>");
document.write(" =");
document.write("<br/>");
document.write("<br/>");
答案 0 :(得分:0)
我建议您使用较短的数据副本:
var myArray1 = new Array(3)
for (i=0; i < 3; i++)
myArray1[i]=new Array(3);
for (var start = 1, i = 0; i < 3; i++)
for (var j = 0; j < 3; j++, start++)
myArray1[i][j] = start;
var myArray2 = new Array(3)
for (i = 0; i < 3; i++)
myArray2[i] = new Array(3)
for (var i = 0; i < 3; i++)
for (var j = 0; j < 3; j++)
myArray2[i][j] = myArray1[i][j];
console.log(myArray1); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
console.log(myArray2); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]