Tech
You copy and reverse the array to find the last match. `findLast()` searches from the end directly.
Here is a pattern that shows up in almost every codebase:
const lastActive = [... users ]. reverse (). find ( u => u . status === ' active ' );
It works. But it creates a full copy of the array, reverses that copy in place, and then walks it from the start. All to get one element near the end. The bigger the array, the more wasteful this is — and the less obvious the intent becomes to the next reader.
ES2023 added findLast() to handle this di...
Read the full discussion on Dev.to
This article was aggregated from Dev.to. Click to join the conversation.
View on Dev.to