
"The problem with reversing arrays Working with lists in front-end apps (e.g., chat messages, activity logs, form history, etc.) often involves finding the last matching item. Prior to ES2023, you'd typically do something like: const lastError = [...logs].reverse().find(log => log.type === 'error'); Reverses a cloned array, which is inefficient Risky if you forget to clone (.slice()) Less readable and more error-prone"
"If you've ever needed to search an array from the end, you've probably reached for a combination of .slice().reverse().find(...), or looped backward manually. It works, but it's not an elegant solution. We now have a better way: Array.prototype.findLast() and Array.prototype.findLastIndex(). These features let you search arrays from the end without reversing them, which gives you cleaner and more intuitive code."
Array.prototype.findLast() and Array.prototype.findLastIndex() provide backward-search capabilities that start at the end of the array and move backward without reversing or copying the array. They simplify common tasks like finding the last matching log entry or the last unread message and improve performance and safety for large datasets. findLast() accepts the same predicate signature as find(element, index, array) and returns the element when the predicate is true. The methods skip sparse array holes just like find() and treat explicit undefined values differently from holes. These features were introduced as part of ES2023.
Read at Allthingssmitty
Unable to calculate read time
Collection
[
|
...
]