Skip to content

Instantly share code, notes, and snippets.

@susanahernandezd
Last active April 19, 2022 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanahernandezd/2b6ef39bb7cd805f0b9b54aadfeaeff9 to your computer and use it in GitHub Desktop.
Save susanahernandezd/2b6ef39bb7cd805f0b9b54aadfeaeff9 to your computer and use it in GitHub Desktop.
// 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