Saif Mujawar
@webbysaif
ES2023 introduces new
array copying methods
to JavaScript.
Saif Mujawar
@webbysaif
toSorted():
The toSorted() method of Array
instances is the copying version
of the sort() method. It returns a
new array without modifying
original array with the elements
sorted in ascending order.
The sort method, on the other
hand, modifies the original array
in place.
Saif Mujawar
@webbysaif
toReversed():
The toReversed() method is used
to create a new array that is the
reverse of the original array.
The original array remains
unchanged.
On the other hand, the reverse
method modifies the original
array in place.
Saif Mujawar
@webbysaif
toSpliced():
The toSpliced() method of Array
instances is the copying version
of the splice() method. It returns
a new array with some elements
removed and/or replaced at a
given index.
The splice() method changes the
contents of an array by removing
or replacing existing elements
and/or adding new elements in
place.
Saif Mujawar
@webbysaif
with():
The with() method is the copying version of using bracket notation to set the value of
an element at a given index in an array. Unlike bracket notation, it doesn't mutate the
array it's called upon.
It returns a new array, allowing you to chain array methods while performing
manipulations without worrying about mutating the original array
Without the .with() method, you would need to copy the array first, mutate the copy,
and then sort the copy
Bonus
Find array from the last
This function will allow us to find element from the last to first of the
array based on a condition.
Turn on notifications
And That's it!!!
Did you find it
useful?
Saif Mujawar
@webbysaif
Follow for more!!