We all have used find
before on an array, but what if we wanted to start from the end of the array? Well, thanks to findLast
you no longer need to perform some weird logic or use an external library to pull this off.
In this example, you can see how findLast
works the same way as find
the only difference is that it starts from the end of the array.
const numbers = [5, 10, 15, 20, 25]
// Find starts from the begining
const n = numbers.find(num => num > 10)
console.log(n) // 15
// Find Last starts from the end
const n = numbers.findLast(num => num > 10)
console.log(n) // 25
Other than IE, you can use findLast
in all major browsers. Check the browser compatibility list for more info.