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()