UNPKG

1.04 kBJavaScriptView Raw
1import baseEachRight from './_baseEachRight';
2import baseFind from './_baseFind';
3import baseFindIndex from './_baseFindIndex';
4import baseIteratee from './_baseIteratee';
5import isArray from './isArray';
6
7/**
8 * This method is like `_.find` except that it iterates over elements of
9 * `collection` from right to left.
10 *
11 * @static
12 * @memberOf _
13 * @since 2.0.0
14 * @category Collection
15 * @param {Array|Object} collection The collection to search.
16 * @param {Array|Function|Object|string} [predicate=_.identity]
17 * The function invoked per iteration.
18 * @returns {*} Returns the matched element, else `undefined`.
19 * @example
20 *
21 * _.findLast([1, 2, 3, 4], function(n) {
22 * return n % 2 == 1;
23 * });
24 * // => 3
25 */
26function findLast(collection, predicate) {
27 predicate = baseIteratee(predicate, 3);
28 if (isArray(collection)) {
29 var index = baseFindIndex(collection, predicate, true);
30 return index > -1 ? collection[index] : undefined;
31 }
32 return baseFind(collection, predicate, baseEachRight);
33}
34
35export default findLast;