_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
13
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
_.findLastIndex与_.findIndex类似,区别是遍历的方向是从右往左
参数
array (Array): 查找的数组
[predicate=_.identity] (Function): 循环调用的函数
[fromIndex=0] (number): 查找的起始位置
返回值
(number): 返回符合要求的元素的索引
例子
var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false } ]; _.findLastIndex(users, function(o) { return o.user == 'pebbles'; }); // => 2 // The `_.matches` iteratee shorthand. _.findLastIndex(users, { 'user': 'barney', 'active': true }); // => 0 // The `_.matchesProperty` iteratee shorthand. _.findLastIndex(users, ['active', false]); // => 2 // The `_.property` iteratee shorthand. _.findLastIndex(users, 'active'); // => 0
源代码:
以下源代码是原始代码,最终生成的代码的predicate参数是经过baseIteratee处理的,predicate用baseIteratee处理成函数,predicate可以有四种形式:一个函数,一个对象来深层比较是否相等,一个两个元素的数组来深层查找是否有相等的属性值,一个属性名来深层查找是否有对应属性名。这都是baseIteratee处理后的结果。
之前看过baseIteratee的实现,这里省略。
import baseFindIndex from './.internal/baseFindIndex.js' /** * This method is like `findIndex` except that it iterates over elements * of `collection` from right to left. * * @since 2.0.0 * @category Array * @param {Array} array The array to inspect. * @param {Function} predicate The function invoked per iteration. * @param {number} [fromIndex=array.length-1] The index to search from. * @returns {number} Returns the index of the found element, else `-1`. * @see find, findIndex, findKey, findLast, findLastKey * @example * * const users = [ * { 'user': 'barney', 'active': true }, * { 'user': 'fred', 'active': false }, * { 'user': 'pebbles', 'active': false } * ] * * findLastIndex(users, ({ user }) => user == 'pebbles') * // => 2 */ //与findIndex类似,区别是遍历的方向是从右往左 function findLastIndex(array, predicate, fromIndex) { const length = array == null ? 0 : array.length//数组长度 if (!length) {//如果数组长度为0,返回-1 return -1 } let index = length - 1//循环索引 if (fromIndex !== undefined) {//处理有fromIndex的情况 //fromIndex最小为0,最大为length - 1 index = fromIndex < 0 ? Math.max(length + fromIndex, 0) : Math.min(fromIndex, length - 1) } return baseFindIndex(array, predicate, index, true)//调用baseFindIndex } export default findLastIndex
下面是baseFindIndex
/** * The base implementation of `findIndex` and `findLastIndex`. * * @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的基础实现 //array查找的数组,predicate每次循环调用的函数,fromIndex遍历的起始位置,fromRight是否反向查找 function baseFindIndex(array, predicate, fromIndex, fromRight) { const { length } = array//数组长度 let index = fromIndex + (fromRight ? 1 : -1)//循环索引,如果反向循环就加1,如果正向就减1 while ((fromRight ? index-- : ++index < length)) {//正向就++,反向-- if (predicate(array[index], index, array)) {//调用predicate,根据其返回的真假值,如果返回true说明找到了 return index } } return -1//找不到返回-1 } export default baseFindIndex