Skip to content

Instantly share code, notes, and snippets.

@KevinPy
Created November 28, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinPy/54cb183c8b72e73887e094007941ee4a to your computer and use it in GitHub Desktop.
Save KevinPy/54cb183c8b72e73887e094007941ee4a to your computer and use it in GitHub Desktop.
Transform Array of Array => Horizontal to Vertical
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const rotate = arr.reduce((value, row) => {
return row.map((_, i) => {
return [...(value[i] || []), row[i]];
});
}, []);
console.log(rotate);
/*
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment