Fwd: Feature proposal
Dmitry Shulgin
shulhindvst at gmail.com
Wed Jul 18 09:17:30 UTC 2018
---------- Forwarded message ----------
From: Dmitry Shulgin <shulhindvst at gmail.com>
Date: 2018-07-05 10:36 GMT+03:00
Subject: Feature proposal
To: es-discuss at mozilla.org
I came across a task of finding an index of the last element in array that
satisfies the condition, so i reversed array and found it by *findIndex *
function.
I was thinking:
Why do we have *findIndex*` method, but no *findLastIndex*?
It seems logical to follow *[index|lastIndex]Of* pair, doesn't it?
Reversing array in this case seems too complicated to me.
So, i would like to suggest new method like
*Array.prototype.findLastIndex()*
Example:
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
console.log([7, 4, 6, 7, 12].findIndexLast(isPrime)); // 3
Would be glad to implement, if it makes sense.
Thx for replies.
P.S. Small issue on GitHub was closed due to offtop (not an issue, as i
understand).
https://github.com/tc39/ecma262/issues/1253
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180718/1d27977a/attachment.html>
More information about the es-discuss
mailing list