Feature proposal

T.J. Crowder tj.crowder at farsightsoftware.com
Wed Jul 18 18:00:40 UTC 2018


On Wed, Jul 18, 2018 at 6:31 PM, Michael Luder-Rosefield <
rosyatrandom at gmail.com> wrote:
> 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.

Yeah, I had a similar thought. We're talking arrays, not generic iterables,
after all.

My thought was some kind of reversed view of the array, something like this:

```js
const reversedView = array => new Proxy(array, {
    get(target, prop, receiver) {
        if (isArrayIndex(prop)) {
            prop = target.length - 1 - prop;
        }
        return Reflect.get(target, prop, receiver);
    }
});
```

...but thought-through, tested, and efficient (which the above Is Not -- it
does work in a basic couple of checks, but I'm running out the door and
wouldn't be surprised if there are edge cases not handled:
http://jsfiddle.net/80se3xqj/). Then my mind sort of wanders off wanting to
generalize this concept of a view on an array... :-)

Amusingly, this doesn't help at all with the original request here:
`findLastIndex` (because it would return the index in the reversed view,
not the original array). But it does with `forEach`, `find`, `some`, etc.:

```js
const objects = [
    {id: 1, type: 1},
    {id: 2, type: 2},
    {id: 3, type: 2},
    {id: 4, type: 3}
];
console.log(reversedView(objects).find(o => o.type === 2));
```

Finds `{id: 3, type: 2}`.

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180718/bd3d6f34/attachment-0001.html>


More information about the es-discuss mailing list