Array.prototype.findIndex()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
Array
实例的 findIndex()
方法返回数组中满足所提供的测试函数的第一个元素的索引。如果没有元素满足测试函数,则返回-1。
¥The findIndex()
method of Array
instances returns the index of the first element in an array that satisfies the provided testing function.
If no elements satisfy the testing function, -1 is returned.
另请参见 find()
方法,该方法返回满足测试函数的第一个元素(而不是其索引)。
¥See also the find()
method, which returns the first element that satisfies the testing function (rather than its index).
Try it
语法
参数
返回值
描述
¥Description
findIndex()
是 迭代法。它按升序索引顺序为数组中的每个元素调用一次提供的 callbackFn
函数,直到 callbackFn
返回 truthy 值。然后 findIndex()
返回该元素的索引并停止遍历数组。如果 callbackFn
从未返回真值,则 findIndex()
返回 -1
。请阅读 迭代法 部分,了解有关这些方法一般如何工作的更多信息。
¥The findIndex()
is an iterative method. It calls a provided callbackFn
function once for each element in an array in ascending-index order, until callbackFn
returns a truthy value. findIndex()
then returns the index of that element and stops iterating through the array. If callbackFn
never returns a truthy value, findIndex()
returns -1
. Read the iterative methods section for more information about how these methods work in general.
callbackFn
会针对数组的每个索引调用,而不仅仅是那些已分配值的索引。稀疏数组 中的空插槽的行为与 undefined
相同。
¥callbackFn
is invoked for every index of the array, not just those with assigned values. Empty slots in sparse arrays behave the same as undefined
.
findIndex()
方法是 generic。它只期望 this
值具有 length
属性和整数键控属性。
¥The findIndex()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.
示例
查找数组中素数的索引
¥Find the index of a prime number in an array
以下示例返回数组中第一个素数元素的索引,如果没有素数则返回 -1
。
¥The following example returns the index of the first element in the array that is a prime number, or -1
if there is no prime number.
function isPrime(element) {
if (element % 2 === 0 || element < 2) {
return false;
}
for (let factor = 3; factor <= Math.sqrt(element); factor += 2) {
if (element % factor === 0) {
return false;
}
}
return true;
}
console.log([4, 6, 8, 9, 12].findIndex(isPrime)); // -1, not found
console.log([4, 6, 7, 9, 12].findIndex(isPrime)); // 2 (array[2] is 7)
使用 callbackFn 的第三个参数
¥Using the third argument of callbackFn
如果你想访问数组中的另一个元素,特别是当你没有引用该数组的现有变量时,array
参数非常有用。以下示例首先使用 filter()
提取正值,然后使用 findIndex()
查找第一个小于其邻居的元素。
¥The array
argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first uses filter()
to extract the positive values and then uses findIndex()
to find the first element that is less than its neighbors.
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const firstTrough = numbers
.filter((num) => num > 0)
.findIndex((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(firstTrough); // 1
在稀疏数组上使用 findIndex()
对非数组对象调用 findIndex()
¥Calling findIndex() on non-array objects
findIndex()
方法读取 this
的 length
属性,然后访问键为小于 length
的非负整数的每个属性。
¥The findIndex()
method reads the length
property of this
and then accesses each property whose key is a nonnegative integer less than length
.
const arrayLike = {
length: 3,
"-1": 0.1, // ignored by findIndex() since -1 < 0
0: 2,
1: 7.3,
2: 4,
};
console.log(
Array.prototype.findIndex.call(arrayLike, (x) => !Number.isInteger(x)),
); // 1
规范
Specification |
---|
ECMAScript Language Specification # sec-array.prototype.findindex |
浏览器兼容性
BCD tables only load in the browser