简体   繁体   English

Underscore.js findLast()方法

[英]Underscore.js findLast() method

Does Underscore.js have a findLast() method or equivalent? findLast()是否有findLast()方法或等效方法?

What is the best way to do .find() but return the last item that matches in Collection? .find()的最佳方法是什么,但返回Collection中匹配的最后一项?

Reverse the list and then use find : 反转列表然后使用find

_.find(list.reverse(), iterator);

Read MDN for the documentation on reverse . 请阅读MDN以获取reverse的文档。


Unfortunately a collection in underscore may be either an array or an object. 不幸的是,下划线中的集合可以是数组或对象。 If your collection is an array then you're in luck. 如果你的收藏是一个阵列,那么你很幸运。 You can use reverse . 你可以使用reverse However if it's an object then you'll need to do this instead: 但是,如果它是一个对象,那么你需要这样做:

_.find(Object.keys(list).reverse(), function (key) {
    return iterator(list[key], key, list);
});

You could write a findLast function for yourself: 你可以为自己编写一个findLast函数:

_.mixin({
    findLast: function (list, iterator, context) {
        if (list instanceof Array)
            return _.find(list.slice(0).reverse(), iterator, context);
        else return _.find(Object.keys(list).reverse(), function (key) {
            return iterator.call(context, list[key], key, list);
        });
    }
});

Now you can use findLast like any other underscore method. 现在你可以像任何其他下划线方法一样使用findLast

Using reverse , as suggested by @AaditMShah, is the easiest solution, but be aware that it manipulates the array in place . 正如@AaditMShah所建议的那样,使用reverse是最简单的解决方案,但请注意它会在适当的位置操作数组。 If you need to preserve the order of elements, you'd have to call reverse a second time, after you are done. 如果你需要保留元素的顺序,你必须在完成后第二次调用reverse

If you don't want to use reverse , you can 如果您不想使用reverse ,则可以

  • use Lodash instead, which provides _.findLast 使用Lodash代替,它提供_.findLast
  • grab the relevant code from Lodash, spread out over findLast and forEachRight and make your own findLast. 从Lodash获取相关代码,分散在findLastforEachRight并制作自己的findLast。

This is what it looks like if you only deal with arrays and don't care about objects: 如果您只处理数组并且不关心对象,那么这就是它的样子:

function findLast (array, callback, thisArg) {
    var index = array.length,
        last;

    callback = callback && typeof thisArg == 'undefined' ? callback : _.bind(callback, thisArg);

    while (index--) {
        if (callback(array[index], index, array) == true) {
            last = array[index];
            break;
        }
    }

    return last;
}

(It works, but I haven't tested it properly. So to anyone reading this, please run a few tests first and don't just copy the code.) (它有效,但我还没有正确测试过。所以对于读过这个的人来说,请先运行一些测试,不要只复制代码。)

Underscore 1.8.0 introduced a method findLastIndex which can be used to accomplish this. Underscore 1.8.0引入了一个方法findLastIndex ,可用于实现此目的。

var numbers = [1, 2, 3, 4];
var index = _.findLastIndex(numbers, isOddNumber);
if (index > 0) { console.log(numbers[index]); }
// returns 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM