Array.prototype.findLast()
findLast() 方法用于 Array 实例,它会反向遍历数组,并返回满足所提供测试函数的第一个元素的值。如果没有元素满足测试函数,则返回 undefined。
如果你需要查找
- 匹配的第一个元素,请使用
find()。 - 数组中最后一个匹配元素的索引,请使用
findLastIndex()。 - 某个值的索引,请使用
indexOf()。(它与findIndex()类似,但会检查每个元素是否与该值相等,而不是使用测试函数。) - 数组中某个值是否存在,请使用
includes()。同样,它会检查每个元素是否与该值相等,而不是使用测试函数。 - 是否有任何元素满足提供的测试函数,请使用
some()。
试一试
const array = [5, 12, 50, 130, 44];
const found = array.findLast((element) => element > 45);
console.log(found);
// Expected output: 130
语法
findLast(callbackFn)
findLast(callbackFn, thisArg)
参数
callbackFnthisArg可选-
在执行
callbackFn时用作this的值。请参阅 迭代方法。
返回值
数组中满足所提供测试函数的最后一个(索引最高)元素;如果没有找到匹配的元素,则为 undefined。
描述
findLast() 方法是一个迭代方法。它会按照索引从高到低的顺序,为数组中的每个元素调用一次所提供的 callbackFn 函数,直到 callbackFn 返回一个真值。然后 findLast() 返回该元素并停止遍历数组。如果 callbackFn 从未返回真值,findLast() 将返回 undefined。有关这些方法通常如何工作的更多信息,请阅读迭代方法部分。
callbackFn 会为数组的每一个索引调用,而不仅仅是那些有赋值的索引。对于稀疏数组中的空槽,其行为与 undefined 相同。
findLast() 方法是通用的。它只要求 this 值具有 length 属性和整数键属性。
示例
根据元素属性查找数组中的最后一个对象
此示例演示了如何基于数组元素的属性创建测试。
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "fish", quantity: 1 },
{ name: "cherries", quantity: 5 },
];
// return true inventory stock is low
function isNotEnough(item) {
return item.quantity < 2;
}
console.log(inventory.findLast(isNotEnough));
// { name: "fish", quantity: 1 }
使用箭头函数和解构赋值
前面的示例可以使用箭头函数和对象解构来编写
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "fish", quantity: 1 },
{ name: "cherries", quantity: 5 },
];
const result = inventory.findLast(({ quantity }) => quantity < 2);
console.log(result);
// { name: "fish", quantity: 1 }
查找数组中的最后一个素数
以下示例返回数组中最后一个素数,或者在没有素数时返回 undefined。
function isPrime(n) {
if (n < 2) {
return false;
}
if (n % 2 === 0) {
return n === 2;
}
for (let factor = 3; factor * factor <= n; factor += 2) {
if (n % factor === 0) {
return false;
}
}
return true;
}
console.log([4, 6, 8, 12].findLast(isPrime)); // undefined, not found
console.log([4, 5, 7, 8, 9, 11, 12].findLast(isPrime)); // 11
注意:isPrime() 实现仅用于演示。对于实际应用,您需要使用高度记忆化的算法,例如埃拉托斯特尼筛法,以避免重复计算。
使用 callbackFn 的第三个参数
当你想访问数组中的另一个元素时,array 参数非常有用,尤其是在没有现有变量引用该数组时。以下示例首先使用 filter() 提取正数,然后使用 findLast() 查找小于其邻居的最后一个元素。
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const lastTrough = numbers
.filter((num) => num > 0)
.findLast((num, idx, arr) => {
// Without the arr argument, there's no way to easily access the
// intermediate array without saving it to a variable.
if (idx > 0 && num >= arr[idx - 1]) return false;
if (idx < arr.length - 1 && num >= arr[idx + 1]) return false;
return true;
});
console.log(lastTrough); // 2
在稀疏数组上使用 findLast()
稀疏数组中的空槽会被访问,并被视为与 undefined 相同。
// Declare array with no elements at indexes 2, 3, and 4
const array = [0, 1, , , , 5, 6];
// Shows all indexes, not just those with assigned values
array.findLast((value, index) => {
console.log(`Visited index ${index} with value ${value}`);
return false;
});
// Visited index 6 with value 6
// Visited index 5 with value 5
// Visited index 4 with value undefined
// Visited index 3 with value undefined
// Visited index 2 with value undefined
// Visited index 1 with value 1
// Visited index 0 with value 0
// Shows all indexes, including deleted
array.findLast((value, index) => {
// Delete element 5 on first iteration
if (index === 6) {
console.log(`Deleting array[5] with value ${array[5]}`);
delete array[5];
}
// Element 5 is still visited even though deleted
console.log(`Visited index ${index} with value ${value}`);
return false;
});
// Deleting array[5] with value 5
// Visited index 6 with value 6
// Visited index 5 with value undefined
// Visited index 4 with value undefined
// Visited index 3 with value undefined
// Visited index 2 with value undefined
// Visited index 1 with value 1
// Visited index 0 with value 0
在非数组对象上调用 findLast()
findLast() 方法会读取 this 的 length 属性,然后访问所有键为小于 length 的非负整数的属性。
const arrayLike = {
length: 3,
0: 2,
1: 7.3,
2: 4,
3: 3, // ignored by findLast() since length is 3
};
console.log(
Array.prototype.findLast.call(arrayLike, (x) => Number.isInteger(x)),
); // 4
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-array.prototype.findlast |
浏览器兼容性
加载中…
另见
core-js中Array.prototype.findLast的 Polyfilles-shims的Array.prototype.findLastPolyfill- 索引集合指南
ArrayArray.prototype.find()Array.prototype.findIndex()Array.prototype.findLastIndex()Array.prototype.includes()Array.prototype.filter()Array.prototype.every()Array.prototype.some()TypedArray.prototype.findLast()