I think this is slightly more readable. It uses Array.from
and logic is identical to using nested loops:
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_
If you are dealing with arrays of unequal length you need to replace arr[0].length
with something else:
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_