I know there's already answer for a similar question, but the outcome is not what I'm looking for.
I want to have [0,1,2,3,4,5,6,7,8] inserted into a multi-dimensional array like:
[0,3,6],[1,4,7],[2,5,8]
instead of:
[0,1,2],[3,4,5],[6,7,8]
const toMatrix = (arr, width) =>
arr.reduce((rows, key, index) => (index % width == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows, []);
Other than using for...loop, is there a shorter way to do it?