UNPKG

1.26 kBJavaScriptView Raw
1import baseFindIndex from './_baseFindIndex';
2import baseIteratee from './_baseIteratee';
3
4/**
5 * This method is like `_.findIndex` except that it iterates over elements
6 * of `collection` from right to left.
7 *
8 * @static
9 * @memberOf _
10 * @since 2.0.0
11 * @category Array
12 * @param {Array} array The array to search.
13 * @param {Array|Function|Object|string} [predicate=_.identity]
14 * The function invoked per iteration.
15 * @returns {number} Returns the index of the found element, else `-1`.
16 * @example
17 *
18 * var users = [
19 * { 'user': 'barney', 'active': true },
20 * { 'user': 'fred', 'active': false },
21 * { 'user': 'pebbles', 'active': false }
22 * ];
23 *
24 * _.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
25 * // => 2
26 *
27 * // The `_.matches` iteratee shorthand.
28 * _.findLastIndex(users, { 'user': 'barney', 'active': true });
29 * // => 0
30 *
31 * // The `_.matchesProperty` iteratee shorthand.
32 * _.findLastIndex(users, ['active', false]);
33 * // => 2
34 *
35 * // The `_.property` iteratee shorthand.
36 * _.findLastIndex(users, 'active');
37 * // => 0
38 */
39function findLastIndex(array, predicate) {
40 return (array && array.length)
41 ? baseFindIndex(array, baseIteratee(predicate, 3), true)
42 : -1;
43}
44
45export default findLastIndex;