Last active
March 2, 2022 12:54
-
-
Save battmanz/a400ad93d9922fdc3a2ff87c0bf7da68 to your computer and use it in GitHub Desktop.
Functions that can be used in place of array mutator methods.
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
| 'use strict'; | |
| const a = Object.freeze([4, 5, 6]); | |
| // Instead of: a.push(7, 8, 9); | |
| const b = a.concat(7, 8, 9); | |
| // Instead of: a.pop(); | |
| const c = a.slice(0, -1); | |
| // Instead of: a.unshift(1, 2, 3); | |
| const d = [1, 2, 3].concat(a); | |
| // Instead of: a.shift(); | |
| const e = a.slice(1); | |
| // Instead of: a.sort(myCompareFunction); | |
| const f = R.sort(myCompareFunction, a); // R = Ramda | |
| // Instead of: a.reverse(); | |
| const g = R.reverse(a); // R = Ramda | |
| // Exercise for the reader: | |
| // copyWithin | |
| // fill | |
| // splice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment