_.lastIndexOf(array, value, [fromIndex=array.length-1])
26
_.lastIndexOf(array, value, [fromIndex=array.length-1])
_.lastIndexOf类似indexOf方法,从右往左寻找元素,然后返回元素的索引
参数
array (Array): 需要查找元素的数组
value (*): 要查找的值
[fromIndex=array.length-1] (number): 查找起始索引
返回值
(number):返回查找到的元素的索引
例子
_.lastIndexOf([1, 2, 1, 2], 2); // => 3 // Search from the `fromIndex`. _.lastIndexOf([1, 2, 1, 2], 2, 2); // => 1
源代码:
/** * lodash (Custom Build) <https://lodash.com/> * Build: `lodash modularize exports="npm" -o ./` * Copyright jQuery Foundation and other contributors <https://jquery.org/> * Released under MIT license <https://lodash.com/license> * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors */ /** Used as references for various `Number` constants. */ var INFINITY = 1 / 0, MAX_INTEGER = 1.7976931348623157e+308, NAN = 0 / 0; /** `Object#toString` result references. */ var symbolTag = '[object Symbol]'; /** Used to match leading and trailing whitespace. */ var reTrim = /^\s+|\s+$/g; /** Used to detect bad signed hexadecimal string values. */ var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; /** Used to detect binary string values. */ var reIsBinary = /^0b[01]+$/i; /** Used to detect octal string values. */ var reIsOctal = /^0o[0-7]+$/i; /** Built-in method references without a dependency on `root`. */ var freeParseInt = parseInt; /** * The base implementation of `_.findIndex` and `_.findLastIndex` without * support for iteratee shorthands. * * @private * @param {Array} array The array to inspect. * @param {Function} predicate The function invoked per iteration. * @param {number} fromIndex The index to search from. * @param {boolean} [fromRight] Specify iterating from right to left. * @returns {number} Returns the index of the matched value, else `-1`. */ //findIndex和findLastIndex的基础实现 function baseFindIndex(array, predicate, fromIndex, fromRight) { var length = array.length,//数组长度 index = fromIndex + (fromRight ? 1 : -1);//处理起始索引 //循环从左往右,减1,循环从右往左,加1 while ((fromRight ? index-- : ++index < length)) {//遍历数组,调用predicate,根据返回的真和假返回索引 if (predicate(array[index], index, array)) { return index; } } return -1;//未找到返回-1 } /** * The base implementation of `_.isNaN` without support for number objects. * * @private * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`. */ //判断一个值是否是NaN function baseIsNaN(value) { return value !== value; } /** Used for built-in method references. */ var objectProto = Object.prototype; /** * Used to resolve the * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) * of values. */ var objectToString = objectProto.toString; /* Built-in method references for those with the same name as other `lodash` methods. */ 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 _ * @since 0.1.0 * @category Array * @param {Array} array The array to inspect. * @param {*} value The value to search for. * @param {number} [fromIndex=array.length-1] The index to search from. * @returns {number} Returns the index of the matched value, else `-1`. * @example * * _.lastIndexOf([1, 2, 1, 2], 2); * // => 3 * * // Search from the `fromIndex`. * _.lastIndexOf([1, 2, 1, 2], 2, 2); * // => 1 */ //类似indexOf方法,从右往左寻找元素,然后返回元素的索引 function lastIndexOf(array, value, fromIndex) { var length = array ? array.length : 0;//数组长度 if (!length) {//如果数组长度为0,返回-1 return -1; } var index = length;//查询起始索引 if (fromIndex !== undefined) {//传递了查询的起始索引参数 index = toInteger(fromIndex);//index赋值为转变成整数的fromIndex index = ( index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1) ) + 1; //如果index小于0,length + index和0里面取一个最大值 //如果index大于0,index和length - 1里面取一个最小值 //上面的处理结束后加1 } if (value !== value) {//如果value是NaN,调用baseFindIndex返回结果,用baseIsNaN判断是否是NaN return baseFindIndex(array, baseIsNaN, index - 1, true); } while (index--) {//如果value不是NaN,循环数组,用严格等于判断是否相等,找到元素后返回其索引 if (array[index] === value) { return index; } } return -1; } /** * Checks if `value` is the * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) * * @static * @memberOf _ * @since 0.1.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is an object, else `false`. * @example * * _.isObject({}); * // => true * * _.isObject([1, 2, 3]); * // => true * * _.isObject(_.noop); * // => true * * _.isObject(null); * // => false */ //判断一个值是否是对象,object或者function类型 function isObject(value) { var type = typeof value; return !!value && (type == 'object' || type == 'function'); } /** * Checks if `value` is object-like. A value is object-like if it's not `null` * and has a `typeof` result of "object". * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is object-like, else `false`. * @example * * _.isObjectLike({}); * // => true * * _.isObjectLike([1, 2, 3]); * // => true * * _.isObjectLike(_.noop); * // => false * * _.isObjectLike(null); * // => false */ //判断一个值是否是一个object-like对象 function isObjectLike(value) { return !!value && typeof value == 'object'; } /** * Checks if `value` is classified as a `Symbol` primitive or object. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. * @example * * _.isSymbol(Symbol.iterator); * // => true * * _.isSymbol('abc'); * // => false */ //判断一个值是否是原生的Symbol类型 function isSymbol(value) { return typeof value == 'symbol' || (isObjectLike(value) && objectToString.call(value) == symbolTag); } /** * Converts `value` to a finite number. * * @static * @memberOf _ * @since 4.12.0 * @category Lang * @param {*} value The value to convert. * @returns {number} Returns the converted number. * @example * * _.toFinite(3.2); * // => 3.2 * * _.toFinite(Number.MIN_VALUE); * // => 5e-324 * * _.toFinite(Infinity); * // => 1.7976931348623157e+308 * * _.toFinite('3.2'); * // => 3.2 */ //将一个值转换成 有限 的数字 function toFinite(value) { if (!value) {//如果value为假,返回0 return value === 0 ? value : 0; } value = toNumber(value);//将value转换成number类型 if (value === INFINITY || value === -INFINITY) {//如果是无限 var sign = (value < 0 ? -1 : 1);//正负标记 return sign * MAX_INTEGER;//转换成有限值 } return value === value ? value : 0;//返回结果,如果是NaN就返回0 } /** * Converts `value` to an integer. * * **Note:** This method is loosely based on * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger). * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to convert. * @returns {number} Returns the converted integer. * @example * * _.toInteger(3.2); * // => 3 * * _.toInteger(Number.MIN_VALUE); * // => 0 * * _.toInteger(Infinity); * // => 1.7976931348623157e+308 * * _.toInteger('3.2'); * // => 3 */ //将一个值转换成整数 function toInteger(value) { var result = toFinite(value),//将value转换成有限值 remainder = result % 1;//取余获取到小数部分 return result === result ? (remainder ? result - remainder : result) : 0; //如果是NaN返回0,如果是小数,减去小数部分,如果没有小数部分直接返回 } /** * Converts `value` to a number. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to process. * @returns {number} Returns the number. * @example * * _.toNumber(3.2); * // => 3.2 * * _.toNumber(Number.MIN_VALUE); * // => 5e-324 * * _.toNumber(Infinity); * // => Infinity * * _.toNumber('3.2'); * // => 3.2 */ //将一个值转换成数字 function toNumber(value) { if (typeof value == 'number') {//如果是number之间返回 return value; } if (isSymbol(value)) {//如果是Symbol类型,返回NaN return NAN; } if (isObject(value)) {//如果value是对象 var other = typeof value.valueOf == 'function' ? value.valueOf() : value; //如果value有valueOf属性,就调用valueOf()取值,否则取原值 value = isObject(other) ? (other + '') : other; //如果other是对象,就转成字符串,否则取原值 } if (typeof value != 'string') {//value被转成字符串后的处理 return value === 0 ? value : +value; } value = value.replace(reTrim, '');//正则把空白字符都转换成空字符串 var isBinary = reIsBinary.test(value);//判断是否是二进制 return (isBinary || reIsOctal.test(value)) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : (reIsBadHex.test(value) ? NAN : +value); //如果是二进制或者八进制,调用parseInt转换 //否则判断不良签名的十六进制转为NaN } module.exports = lastIndexOf;

浙公网安备 33010602011771号