Flip the list and then use find :
_.find(list.reverse(), iterator);
Read the MDN for reverse documentation.
Unfortunately, a collection with an underscore can be either an array or an object. If your collection is an array, then you are in luck. You can use reverse . However, if it is an object, you will need to do this instead:
_.find(Object.keys(list).reverse(), function (key) { return iterator(list[key], key, list); });
You can write findLast function for yourself:
_.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.
source share