Array findLast() and findLastIndex()

Sebastian Malton sebastian at
Fri Aug 4 19:26:41 UTC 2017

After some research iterators cannot be used because of lack of functions on them. However, for brevity and efficiency these sorts of functions should still exist.


  Original Message  
From: kdex at
Sent: August 4, 2017 3:10 PM
To: es-discuss at
Subject: Re: Array findLast() and findLastIndex()

I'm thinking that this may have performance benefits. The naïve approach would 
be something like:

function findLast(array, fn) {
return array.reverse().find(fn);

Note that this requires a reversed copy of `array` to reside in memory before 
`find` can be run, which is not a particularly useful thing to do when all you 
want is to start the search from another direction.

Instead, you would have to do something like:

function findLast(array, predicate) {
for (let i = array.length - 1; i >= 0; --i) {
const x = array[i];
if (predicate(x)) {
return x;

which does the job, I guess. Apart from performance considerations, I would 
find it practical if all methods that work in *some* direction had a 
counterpart for the other direction, c.f. `trim{Start,End}`, `pad{Start,End}`, 
`reduce{,Right}`. It would be consistent, would it not?

On Friday, August 4, 2017 8:35:52 PM CEST Sebastian Malton wrote:
> I don't specifically have an exact use case but I can definitely think of
> some use cases for them. However, if these sorts of functions also work for
> array iterators then a reverse entries functions should suffice.
> Sebastian 
> From:  ljharb at
> Sent: August 4, 2017 2:26 PM
> To:  sebastian at
> Cc: es-discuss at
> Subject: Re: Array findLast() and findLastIndex()
> indexOf and reduce are the only iteration methods that have "reverse"
> versions - I think to add any others, a clear case would have to be made
> that *all* of them shouldn't get the same treatment.
> What's your use case?
> On Fri, Aug 4, 2017 at 11:19 AM, Sebastian Malton <sebastian at> 
> wrote: This function would be like find but would iterate from back to
> front. We already have find/findIndex but unlike indexOf they don't have a
> last counterpart.
> Sebastian 
> _______________________________________________
> es-discuss mailing list
> es-discuss at
es-discuss mailing list
es-discuss at

More information about the es-discuss mailing list