[javascript] Transposing a 2D-array in JavaScript

I think this is slightly more readable. It uses Array.from and logic is identical to using nested loops:

_x000D_
_x000D_
var arr = [_x000D_
  [1, 2, 3, 4],_x000D_
  [1, 2, 3, 4],_x000D_
  [1, 2, 3, 4]_x000D_
];_x000D_
_x000D_
/*_x000D_
 * arr[0].length = 4 = number of result rows_x000D_
 * arr.length = 3 = number of result cols_x000D_
 */_x000D_
_x000D_
var result = Array.from({ length: arr[0].length }, function(x, row) {_x000D_
  return Array.from({ length: arr.length }, function(x, col) {_x000D_
    return arr[col][row];_x000D_
  });_x000D_
});_x000D_
_x000D_
console.log(result);
_x000D_
_x000D_
_x000D_

If you are dealing with arrays of unequal length you need to replace arr[0].length with something else:

_x000D_
_x000D_
var arr = [_x000D_
  [1, 2],_x000D_
  [1, 2, 3],_x000D_
  [1, 2, 3, 4]_x000D_
];_x000D_
_x000D_
/*_x000D_
 * arr[0].length = 4 = number of result rows_x000D_
 * arr.length = 3 = number of result cols_x000D_
 */_x000D_
_x000D_
var result = Array.from({ length: arr.reduce(function(max, item) { return item.length > max ? item.length : max; }, 0) }, function(x, row) {_x000D_
  return Array.from({ length: arr.length }, function(x, col) {_x000D_
    return arr[col][row];_x000D_
  });_x000D_
});_x000D_
_x000D_
console.log(result);
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to matrix

How to get element-wise matrix multiplication (Hadamard product) in numpy? How can I plot a confusion matrix? Error: stray '\240' in program What does the error "arguments imply differing number of rows: x, y" mean? How to input matrix (2D list) in Python? Difference between numpy.array shape (R, 1) and (R,) Counting the number of non-NaN elements in a numpy ndarray in Python Inverse of a matrix using numpy How to create an empty matrix in R? numpy matrix vector multiplication

Examples related to transpose

Converting rows into columns and columns into rows using R Postgres - Transpose Rows to Columns Transposing a 2D-array in JavaScript What is the fastest way to transpose a matrix in C++? Excel VBA - Range.Copy transpose paste Transpose list of lists Transposing a 1D NumPy array An efficient way to transpose a file in Bash Transpose/Unzip Function (inverse of zip)?