1 | import indexOfNaN from './_indexOfNaN';
|
2 | import toInteger from './toInteger';
|
3 |
|
4 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
5 | var nativeMax = Math.max,
|
6 | nativeMin = Math.min;
|
7 |
|
8 | /**
|
9 | * This method is like `_.indexOf` except that it iterates over elements of
|
10 | * `array` from right to left.
|
11 | *
|
12 | * @static
|
13 | * @memberOf _
|
14 | * @since 0.1.0
|
15 | * @category Array
|
16 | * @param {Array} array The array to search.
|
17 | * @param {*} value The value to search for.
|
18 | * @param {number} [fromIndex=array.length-1] The index to search from.
|
19 | * @returns {number} Returns the index of the matched value, else `-1`.
|
20 | * @example
|
21 | *
|
22 | * _.lastIndexOf([1, 2, 1, 2], 2);
|
23 | * // => 3
|
24 | *
|
25 | * // Search from the `fromIndex`.
|
26 | * _.lastIndexOf([1, 2, 1, 2], 2, 2);
|
27 | * // => 1
|
28 | */
|
29 | function lastIndexOf(array, value, fromIndex) {
|
30 | var length = array ? array.length : 0;
|
31 | if (!length) {
|
32 | return -1;
|
33 | }
|
34 | var index = length;
|
35 | if (fromIndex !== undefined) {
|
36 | index = toInteger(fromIndex);
|
37 | index = (
|
38 | index < 0
|
39 | ? nativeMax(length + index, 0)
|
40 | : nativeMin(index, length - 1)
|
41 | ) + 1;
|
42 | }
|
43 | if (value !== value) {
|
44 | return indexOfNaN(array, index, true);
|
45 | }
|
46 | while (index--) {
|
47 | if (array[index] === value) {
|
48 | return index;
|
49 | }
|
50 | }
|
51 | return -1;
|
52 | }
|
53 |
|
54 | export default lastIndexOf;
|