Skip to content

Instantly share code, notes, and snippets.

@notshekhar
Created May 28, 2021 13:27
Show Gist options
  • Select an option

  • Save notshekhar/92ecc255cc6ee6500f3b243069f178ba to your computer and use it in GitHub Desktop.

Select an option

Save notshekhar/92ecc255cc6ee6500f3b243069f178ba to your computer and use it in GitHub Desktop.
//function to reshape the array
Array.prototype.reshape = function (rows, cols) {
var copy = this.slice(0) // Copy all elements.
this.length = 0 // Clear out existing array.
for (var r = 0; r < rows; r++) {
var row = []
for (var c = 0; c < cols; c++) {
var i = r * cols + c
if (i < copy.length) {
row.push(copy[i])
}
}
this.push(row)
}
}
Array.prototype.copy = function () {
return this.map((e) => e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment