Last active
September 6, 2023 00:28
-
-
Save femto113/1784503 to your computer and use it in GitHub Desktop.
one line transpose for 2-dimensional Javascript arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transpose(a) | |
{ | |
return a[0].map(function (_, c) { return a.map(function (r) { return r[c]; }); }); | |
// or in more modern dialect | |
// return a[0].map((_, c) => a.map(r => r[c])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tatomyr empty arrays (or non-2-dimensional arrays) a legit concern for a library method, not sure if it's important for a one liner. Probably best dealt with something like this