Swap rows with columns (transpose) matrix in javascript

For example, I have a matrix like this:

|1 2 3| |4 5 6| |7 8 9| 

and I need to convert it to such a matrix:

 |1 4 7| |2 5 8| |3 6 9| 

What is the best and best way to achieve this?

+25
javascript matrix swap multidimensional-array
Dec 20 2018-10-18
source share
6 answers

See article: Transpose Array in JavaScript and jQuery

 function transpose(a) { // Calculate the width and height of the Array var w = a.length || 0; var h = a[0] instanceof Array ? a[0].length : 0; // In case it is a zero matrix, no transpose routine needed. if(h === 0 || w === 0) { return []; } /** * @var {Number} i Counter * @var {Number} j Counter * @var {Array} t Transposed data is stored in this array. */ var i, j, t = []; // Loop through every item in the outer array (height) for(i=0; i<h; i++) { // Insert a new row (array) t[i] = []; // Loop through every item per item in outer array (width) for(j=0; j<w; j++) { // Save transposed data. t[i][j] = a[j][i]; } } return t; } console.log(transpose([[1,2,3],[4,5,6],[7,8,9]])); 
+17
Dec 20 2018-10-18
source share

DuckDucking announced it by Ken . Surprisingly, this is even more concise and complete than Nikita's answer . It extracts the lengths of columns and rows implicitly within the gut of map() .

 function transpose(a) { return Object.keys(a[0]).map(function(c) { return a.map(function(r) { return r[c]; }); }); } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ])); 

[[1,4,5], [2,5,8], [7,8,9]

+56
Nov 05
source share

As in any other language:

 int[][] copy = new int[columns][rows]; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { copy[j][i] = original[i][j]; } } 

You just need to build a 2D array in different ways in JS. Like this:

 function transpose(original) { var copy = []; for (var i = 0; i < original.length; ++i) { for (var j = 0; j < original[i].length; ++j) { // skip undefined values to preserve sparse array if (original[i][j] === undefined) continue; // create row if it doesn't exist yet if (copy[j] === undefined) copy[j] = []; // swap the x and y coords for the copy copy[j][i] = original[i][j]; } } return copy; } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ])); 
+6
Dec 20 '10 at 18:34
source share

I don't have enough reputation to comment (wtf.), So I need to post an updated version of Ken as a separate answer:

 function transpose(a) { return a[0].map(function (_, c) { return a.map(function (r) { return r[c]; }); }); } 
+5
Apr 28 '15 at 9:03
source share

Compact version Response to Hobs using arrow functions from ES6:

 function transpose(matrix) { return Object.keys(matrix[0]) .map(colNumber => matrix.map(rowNumber => rowNumber[colNumber])); } 
+2
Nov 13 '16 at 14:50
source share

You can use Object.keys and Array.prototype.map :

 function transpose(arr) { return Object.keys(arr[0]).map(function (c) { return arr.map(function (r) { return r[c]; }); }); } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ])); 
0
Sep 17 '14 at 9:56
source share



All Articles