Feature proposal
Michael Luder-Rosefield
rosyatrandom at gmail.com
Wed Jul 18 17:31:41 UTC 2018
Is strikes me that every single Array method that takes an iteratee
function (signature (value, index, array)) should be able to iterate
through the array in reverse, in a standardised way.
At the moment, we have:
- every
- filter
- find
- findIndex
- forEach
- indexOf / lastIndexOf
- map
- reduce / reduceRight
- some
which is not very consistent at all. Perhaps we could add an `iterOrder`
method that changes the way the array is iterated through?
I propose it could take 1 parameter:
- If -1 is passed in, the array is iterated through in reverse.
- If 1,0 or undefined is passed through, the array is iterated through
normally.
- If an array of numbers is passed through, these represent the indexes
in the order they will be processed.
- Any illegible indexes in the passed-in array will be ignored
- Any eligible indexes not given will be ignored
- If a generator function is passed in, it should take the array (or a
length value) and spit out indexes.
- If a non-generator function is passed in, it should spit out an array
of indexes, which will be used as-per arrays being passed in
Whether that iterOrder is reset after an iteration could go either way. I'd
suggest it probably should, with an option to persist.
Example:
```
let arr = [ 8,4,7,3 ];
arr.iterOrder().map(x => x) === arr
arr.iterOrder(-1).map(x => x) === arr.reverse() === [ 3,7,4,8 ]
arr.iterOrder([ 2,1 ]).map(x => x) === [ 7,4 ]
// haven't got time to give function arg examples, but randomisers would be
fairly trivial
// original indexes are preserved during iteration:
arr.iterOrder(-1).forEach((val, i) => console.log(val, i))
// > 3, 3
// > 7, 2
// > 4, 1
// > 8, 0
```
This is a messy first-pass at the problem, and I can already see potential
issues, but it would at least standardise and generalise the problem
On Wed, 18 Jul 2018 at 17:44 Cyril Auburtin <cyril.auburtin at gmail.com>
wrote:
> 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
>>>
>> _______________________________________________
> 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/25f49a5c/attachment-0001.html>
More information about the es-discuss
mailing list