Open jingchaocheng opened 4 years ago
import baseFindIndex from './.internal/baseFindIndex.js' import toInteger from './toInteger.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 */ function findLastIndex(array, predicate, fromIndex) { // 获取 array length const length = array == null ? 0 : array.length // 如果 length 为 0 返回 -1 if (!length) { return -1 } // array 的最大索引 let index = length - 1 // 如果没有传入 fromIndex 参数 if (fromIndex !== undefined) { // fromIndex 转 int index = toInteger(fromIndex) // 获取有效的 index index = fromIndex < 0 ? Math.max(length + index, 0) : Math.min(index, length - 1) } return baseFindIndex(array, predicate, index, true) } export default findLastIndex