Files
second-brain/Clippings/Nobody Wants To Use These Array Methods😭.md

124 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Nobody Wants To Use These Array Methods😭"
source: "https://medium.com/coding-beauty/nobody-wants-to-use-these-array-methods-957180e597ac"
author:
- "[[Tari Ibaba]]"
published: 2024-10-13
created: 2024-10-29
description: "Array copyWithin() copies a part of an array to another position in the same array and returns it without increasing its length. The cool thing about these new methods is how they let you get and…"
tags:
- "clippings"
---
[
![Tari Ibaba](https://miro.medium.com/v2/resize:fill:88:88/1*hrkjW77qwMoev9kLXYZg5w.png)
](https://medium.com/@tariibaba?source=post_page---byline--957180e597ac--------------------------------)
[
![Coding Beauty](https://miro.medium.com/v2/resize:fill:48:48/1*ViyWUoh4zqx294no1eENxw.png)
](https://medium.com/coding-beauty?source=post_page---byline--957180e597ac--------------------------------)
![](https://miro.medium.com/v2/resize:fit:700/1*rVhnzwU82P5Zv24CWYT00Q.png)
Theres so much more to arrays than `map()`, `filter()`, `find()`, and `push()` .
But most devs are completely clueless about this — several powerful methods theyre missing out on.
Check these out:
## 1\. `copyWithin()`
`Array` `copyWithin()` copies a part of an array to another position in the same array and returns it without increasing its length.
![](https://miro.medium.com/v2/resize:fit:700/1*UI2fKTXWH1qT1RB_8K21KA.png)
`end` parameter is optional:
![](https://miro.medium.com/v2/resize:fit:700/1*EcYWunSj_9xS2etdqw4yfQ.png)
![](https://miro.medium.com/v2/resize:fit:700/1*uYOiZcfBU5qsr5Gn5cpjiQ.png)
## 2\. at() and `with()`
`at()` came first and `with()` came a year after that in 2023.
They are the functional and immutable versions of single-element array modification and access.
![](https://miro.medium.com/v2/resize:fit:700/1*Y22uGYR6l5HkGVq6VOLCqw.png)
The cool thing about these new methods is how they let you get and change element values with negative indexing.
## 3\. `Array` `reduceRight()` method
Works like `reduce()` but the callback goes from right to left instead of left to right:
![](https://miro.medium.com/v2/resize:fit:700/1*NFbUI6qAStv3APmavt93Lg.png)
Heres another great scenario for `reduceRight()`:
![](https://miro.medium.com/v2/resize:fit:700/1*7F4jH5yKoh8OXJZY24Xvog.png)
## 4\. Array `findLast()` method
New in [ES13](https://wp.codingbeautydev.com/es13-javascript-features/): find array item starting from last element.
Great for cases where where searching from end position produces better performance than with `find()`
Example:
![](https://miro.medium.com/v2/resize:fit:700/1*Lw2ceeHdhA6CWfWXVdv_yQ.png)
This works but as our target object is closer to the tail of the array, `findLast()` should run faster:
![](https://miro.medium.com/v2/resize:fit:700/1*2zDP_MxYgnSK3FOe0pNiZw.png)
Another use case for `findLast()` is when we have to specifically search the array from the end to get the correct element.
For example, if we want to find the last even number in a list of numbers, `find()` would produce a totally wrong result:
![](https://miro.medium.com/v2/resize:fit:700/1*kt1frmGkd6gOW6ynuM107A.png)
But `findLast()` will start the search from the end and give us the correct item:
![](https://miro.medium.com/v2/resize:fit:700/1*ueRdCzLJI6l1fS9ERxQBIg.png)
## 5\. `toSorted()`, `toReversed()`, `toSpliced()`
ES2023 came fully packed with immutable versions of `sort()`, `reverse()`, and `splice()`.
Okay maybe `splice()` isn't used as much as the others, but they all mutate the array in place.
![](https://miro.medium.com/v2/resize:fit:700/1*y2DF6_KfCoG33nC9DmdR6Q.png)
Immutability gives us predictable and safer code; debugging is much easier as were certain variables never change their value.
Arguments are exactly the same, with `splice()` and `toSpliced()` having to differ in their return value.
![](https://miro.medium.com/v2/resize:fit:700/1*k6xv-R7x7j9-pn98Fd-NRA.png)
## 6\. Array `lastIndexOf()` method
The `lastIndexOf()` method returns the last index where a particular element can be found in an array.
![](https://miro.medium.com/v2/resize:fit:700/1*lAqwph3lwJB2caVna8RfsQ.png)
We can pass a second argument to `lastIndexOf()` to specify an index in the array where it should stop searching for the string after that index:
![](https://miro.medium.com/v2/resize:fit:700/1*ixivWe5hb336VjZFVI325g.png)
## 7\. Array `flatMap()` method
The `flatMap()` method transforms an array using a given callback function and then flattens the transformed result by one level:
![](https://miro.medium.com/v2/resize:fit:700/1*vrLyoK3wIXniUxBkCInamw.png)
Calling `flatMap()` on the array does the same thing as calling `map()` followed by a `flat()` of depth 1, but it\`s a bit more efficient than calling these two methods separately.
![](https://miro.medium.com/v2/resize:fit:700/1*rWjFGjxLR7ckiJJj7Tx2MQ.png)
## Final thoughts
They are not that well-known (yet) but they have their unique uses and quite powerful.