In sugar.js, how do you find an Array's "last index of" by filtering (not exact match)?

334 views Asked by At

It looks like the original functionality of Array.lastIndexOf was what I'm looking for, but it was changed. Any idea how to replicate the old functionality?

http://blog.sugarjs.com/post/8452749291/v0-9-2-indexof-lastindexof-fixed

1

There are 1 answers

0
Andy On BEST ANSWER

After looking at the sugar.js docs, it appears that Array#findIndex does the reverse of what I'm trying to do, so here's my solution which uses that:

Array.prototype.findLastIndex = function(f, startIndex, loop) {
    var reverseIndex = this.clone().reverse().findIndex(f, startIndex, loop);
    if (reverseIndex < 0) {
        return reverseIndex;
    }
    return this.length - reverseIndex - 1;
};