You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

4 years ago
  1. var baseFindIndex = require('./_baseFindIndex'),
  2. baseIsNaN = require('./_baseIsNaN'),
  3. strictLastIndexOf = require('./_strictLastIndexOf'),
  4. toInteger = require('./toInteger');
  5. /* Built-in method references for those with the same name as other `lodash` methods. */
  6. var nativeMax = Math.max,
  7. nativeMin = Math.min;
  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 inspect.
  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 == null ? 0 : array.length;
  31. if (!length) {
  32. return -1;
  33. }
  34. var index = length;
  35. if (fromIndex !== undefined) {
  36. index = toInteger(fromIndex);
  37. index = index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1);
  38. }
  39. return value === value
  40. ? strictLastIndexOf(array, value, index)
  41. : baseFindIndex(array, baseIsNaN, index, true);
  42. }
  43. module.exports = lastIndexOf;