Skip to content

Instantly share code, notes, and snippets.

@itstanany
Created October 16, 2020 16:00
Show Gist options
  • Save itstanany/3301ab177c243f379ff408da6a50f586 to your computer and use it in GitHub Desktop.
Save itstanany/3301ab177c243f379ff408da6a50f586 to your computer and use it in GitHub Desktop.
Transpose function as an array method
// one line transpose array
// iterate over th elements of the first arra to dtermine the number of rows in the new array
// then iterate over the arrays of the original parent array to determine the number of columns and add the new values to the created array(s)
Array.prototype.Transpose = function() {
return this[0].map((elem1, indx1) => (this.map((elem2) => (elem2[indx1]))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment