Last active
April 19, 2022 04:34
-
-
Save susanahernandezd/2b6ef39bb7cd805f0b9b54aadfeaeff9 to your computer and use it in GitHub Desktop.
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
// Destructive Array methods: | |
//.reverse() | |
//.sort() | |
//.splice() | |
// If we want to apply one of these methods to an Array without changing it, we can use one of the following patterns: | |
// OPTION 1: | |
// That is, we first make a copy of arr and then change that copy. | |
const sorted1 = arr.slice().sort(); | |
const sorted2 = [...arr].sort(); | |
const sorted3 = Array.from(arr).sort(); | |
// OPTION 2 (new!): | |
// Instead of .reverse(), use: | |
.toReversed(): Array | |
// Instead of .sort(), use: | |
.toSorted(compareFn): Array | |
// Instead of .splice(), use: | |
.toSpliced(start, deleteCount, ...items): Array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment