Created
May 28, 2021 13:27
-
-
Save notshekhar/92ecc255cc6ee6500f3b243069f178ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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