Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
Array 實例的 find() 方法會回傳在提供的陣列中第一個通過所提供測試函式的元素。如果沒有任何值通過測試函式,則回傳 undefined。
會對陣列中的每個元素執行的函式。此函式應回傳真值以指出找到符合條件的元素,否則應回傳假值。此函式被呼叫時可以傳入以下引數:
element陣列中目前正在處理的元素。
index陣列中目前正在處理的元素的索引。
array呼叫 find() 的陣列。
thisArg 選擇性執行 callbackFn 時用作 this 的值。請參見迭代方法。
回傳第一個通過測試函式的陣列元素。否則回傳 undefined。
find() 方法是一個迭代方法。它會以索引遞增順序對陣列中的每個元素執行一次所提供的 callbackFn 函式,直到 callbackFn 回傳一個真值。此時,find() 會回傳該元素並停止迭代陣列。若 callbackFn 未曾回傳真值,則 find() 會回傳 undefined。請參閱迭代方法部分以深入了解這些方法的運作方式。
callbackFn 會針對陣列中每個索引執行一次,不僅限於已被賦值的索引。稀疏陣列中的空槽會視為 undefined,行為將會相同。
find() 方法是通用的。它只要求 this 具有 length 屬性以及整數鍵屬性即可。
下例會回傳陣列中第一個質數元素,若找不到質數則回傳 undefined。
array 引數在你沒有另外的變數參照陣列時特別有用,能讓你存取陣列中其他的元素。以下例子中,我們先用 filter() 取出所有正數,再用 find() 找出第一個比其相鄰數值都小的數字。
稀疏陣列中的空槽仍會被訪問,並被視為與 undefined 相同。
find() 方法會讀取 this 的 length 屬性,然後依據非負整數索引存取對應屬性。
| ECMAScript® 2027 Language Specification # sec-array.prototype.find |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.