Using below script we can transpose an m x n matrix array
- <script>
- function transpose(arr) {
- var k = arr.length;
- var l = arr[0].length;
- var arr1 = [];
- var arr2 = [];
- for (var j = 0; j < l; j++) {
- arr1 = [];
- for (var i = 0; i < k; i++) {
- arr1.push(arr[i][j]);
- }
- arr2.push(arr1);
- }
- return arr2;
- }
- var originalArray = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
- var transposedArray = transpose(originalArray);
- </script>