mirror of https://github.com/lukechilds/node.git
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.
50 lines
1.3 KiB
50 lines
1.3 KiB
9 years ago
|
var indexOfNaN = require('./_indexOfNaN'),
|
||
|
toInteger = require('./toInteger');
|
||
10 years ago
|
|
||
9 years ago
|
/* Built-in method references for those with the same name as other `lodash` methods. */
|
||
10 years ago
|
var nativeMax = Math.max,
|
||
|
nativeMin = Math.min;
|
||
|
|
||
|
/**
|
||
|
* This method is like `_.indexOf` except that it iterates over elements of
|
||
|
* `array` from right to left.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @category Array
|
||
|
* @param {Array} array The array to search.
|
||
|
* @param {*} value The value to search for.
|
||
9 years ago
|
* @param {number} [fromIndex=array.length-1] The index to search from.
|
||
10 years ago
|
* @returns {number} Returns the index of the matched value, else `-1`.
|
||
|
* @example
|
||
|
*
|
||
|
* _.lastIndexOf([1, 2, 1, 2], 2);
|
||
|
* // => 3
|
||
|
*
|
||
9 years ago
|
* // Search from the `fromIndex`.
|
||
10 years ago
|
* _.lastIndexOf([1, 2, 1, 2], 2, 2);
|
||
|
* // => 1
|
||
|
*/
|
||
|
function lastIndexOf(array, value, fromIndex) {
|
||
|
var length = array ? array.length : 0;
|
||
|
if (!length) {
|
||
|
return -1;
|
||
|
}
|
||
|
var index = length;
|
||
9 years ago
|
if (fromIndex !== undefined) {
|
||
|
index = toInteger(fromIndex);
|
||
|
index = (index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1)) + 1;
|
||
10 years ago
|
}
|
||
|
if (value !== value) {
|
||
|
return indexOfNaN(array, index, true);
|
||
|
}
|
||
|
while (index--) {
|
||
|
if (array[index] === value) {
|
||
|
return index;
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
module.exports = lastIndexOf;
|