indexOf.js (1240B)
1 var baseIndexOf = require('./_baseIndexOf'), 2 toInteger = require('./toInteger'); 3 4 /* Built-in method references for those with the same name as other `lodash` methods. */ 5 var nativeMax = Math.max; 6 7 /** 8 * Gets the index at which the first occurrence of `value` is found in `array` 9 * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) 10 * for equality comparisons. If `fromIndex` is negative, it's used as the 11 * offset from the end of `array`. 12 * 13 * @static 14 * @memberOf _ 15 * @since 0.1.0 16 * @category Array 17 * @param {Array} array The array to inspect. 18 * @param {*} value The value to search for. 19 * @param {number} [fromIndex=0] The index to search from. 20 * @returns {number} Returns the index of the matched value, else `-1`. 21 * @example 22 * 23 * _.indexOf([1, 2, 1, 2], 2); 24 * // => 1 25 * 26 * // Search from the `fromIndex`. 27 * _.indexOf([1, 2, 1, 2], 2, 2); 28 * // => 3 29 */ 30 function indexOf(array, value, fromIndex) { 31 var length = array == null ? 0 : array.length; 32 if (!length) { 33 return -1; 34 } 35 var index = fromIndex == null ? 0 : toInteger(fromIndex); 36 if (index < 0) { 37 index = nativeMax(length + index, 0); 38 } 39 return baseIndexOf(array, value, index); 40 } 41 42 module.exports = indexOf;