Array findLast() and findLastIndex()

kdex kdex at
Fri Aug 4 19:10:33 UTC 2017

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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <>

More information about the es-discuss mailing list