Javascript 2D array transpose instance, javascript 2D Array
This article describes how to transpose a two-dimensional array of javascript. Share it with you for your reference. The specific implementation method is as follows:
Copy codeThe Code is as follows: <script language = "javascript" type = "text/javascript">
Var arr1 = [[30,-70,100], [,-40], [, 6], [, 9];
Var arr2 = [];
// Determine the number of rows in the new array
For (var I = 0; I <arr1 [0]. length; I ++ ){
Arr2 [I] = [];
}
// Dynamically add data
// Traverse the original array
For (var I = 0; I <arr1.length; I ++ ){
For (var j = 0; j <arr1 [I]. length; j ++ ){
Arr2 [j] [I] = arr1 [I] [j];
}
}
// Print the new array
For (var I = 0; I <arr2.length; I ++ ){
For (var j = 0; j <arr2 [I]. length; j ++ ){
Document. writeln (arr2 [I] [j]);
}
Document. write ("<br/> ");
}
</Script>
I hope this article will help you design javascript programs.