Feature proposal
Cyril Auburtin
cyril.auburtin at gmail.com
Wed Jul 18 16:44:16 UTC 2018
sorry you get 1, and need again to subtract the array length, `arr.length
-1 - 1` to get the final result 3
Le mer. 18 juil. 2018 à 18:41, Cyril Auburtin <cyril.auburtin at gmail.com> a
écrit :
> there you go
> ```
> console.log([7, 4, 6, 7, 12].findIndex((_, i, a) =>
> isPrime(a[a.length-1-i]))); // 3
> ```
>
> Le mer. 18 juil. 2018 à 11:17, Dmitry Shulgin <shulhindvst at gmail.com> a
> écrit :
>
>>
>> ---------- 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
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180718/d9b09a8a/attachment-0001.html>
More information about the es-discuss
mailing list