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