JavaScript TypeArray findIndex() 方法返回当前类型化数组中满足提供的测试函数的第一个元素的索引。如果未满足任何值或没有向 testing 函数传递任何值,则返回 -1。
以下是关于 findIndex() 方法的一些附加要点 -
- findIndex() 方法对 TypedArray(例如 Uint8Array、Int16Array 等)进行操作。
- 它接受 testing 函数作为参数。
- 对 TypedArray 中的每个元素执行 testing 函数。
- 如果元素满足 testing 函数指定的条件(返回 true 值),则返回该元素的索引作为结果。
- 如果未将任何元素传递给 testing 函数,则返回 −1。
语法
以下是 JavaScript TypedArray findIndex() 方法的语法 -
findIndex(callbackFn, thisArg)
参数
此方法接受两个名为 'callbackFn' 和 'thisArg' 的参数,如下所述 -
- callbackFn − 此参数是一个测试函数,将针对 TypedArray 中的每个元素执行。此函数接受名为 'element'、'index' 和 'array' 的三个参数。以下是每个参数的描述 -
- element − 表示 TypedArray 中正在处理的当前元素。
- index − 指示 TypedArray 中当前元素的索引(位置)。
- array − 指整个 TypedArray。
- thisArg (可选) − 这是一个可选参数,允许您在 callbackFn 中指定 this 的值。
返回值
此方法返回满足提供的测试函数的类型化数组中第一个元素的索引,否则返回 '−1'。
示例 1
在下面的示例中,我们使用 JavaScript TypedArray findIndex() 方法搜索原始 TypedArray [1, 2, 3, 4, 5, 6, 7, 8] 中第一个偶数的索引。我们创建一个名为 isEven() 的测试函数来检查数字是否为偶数,然后将此函数作为参数传递给 findIndex() 方法。
<html>
<head>
<title>JavaScript TypedArray findIndex() Method</title>
</head>
<body>
<script>
function isEven(element, index, array){
return element %2 == 0;
}
const T_array = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]);
document.write("Orizinal TypedArray: ", T_array);
document.write("<br>An index of the first even number in TypedArray is: ");
document.write(T_array.findIndex(isEven));
</script>
</body>
</html>
输出
上面的程序返回类型化数组 [1, 2, 3, ,4 ,5 ,6 ,7, 8] 中第一个偶数的索引作为 '1'。
An index of the first even number in TypedArray is: 1
示例 2
以下是 JavaScript TypedArray findIndex() 的另一个示例。在此示例中,我们使用 JavaScript TypedArray findIndex() 方法搜索 TypedArray [1, 2, −1, 0, 1, 2, −2, −3] 中第一个负数的索引。我们创建一个测试函数来检查数字是否为负数,然后将此函数作为参数传递给 findIndex() 方法。
<html>
<head>
<title>JavaScript TypedArray findIndex() Method</title>
</head>
<body>
<script>
function isNegative(element, index, array){
return element < 0;
}
const T_array = new Int8Array([1, 2, -1, 0, 1, 2, -2, -3]);
document.write("Orizinal TypedArray: ", T_array);
document.write("<br>An index of the first negative number in TypedArray is: ");
document.write(T_array.findIndex(isNegative));
</script>
</body>
</html>
输出
执行上述程序后,它返回类型化数组 [1, 2, −1, 0, 1, 2, −2, −3] 中第一个负数的索引 2。
An index of the first negative number in TypedArray is: 2
示例 3
如果没有值满足 testing 函数,它将返回 '−1'。
在这个程序中,我们创建了一个名为 isPrime() 的函数,用于检查一个数字是否是素数。我们使用此函数作为 findIndex() 函数的参数来检索 TypedArray [4, 6, 8, 9] 中第一个素数的索引。但是,由于没有值满足 isPrime() 函数,因此 findIndex() 方法返回 −1。
<html>
<head>
<title>JavaScript TypedArray findIndex() Method</title>
</head>
<body>
<script>
function isPrime(element, index, array){
let begin = 2;
while(begin <= Math.sqrt(element)){
if(element % begin++ < 1){
return false;
}
}
return element > 1;
}
const T_array = new Int8Array([4, 6, 8, 9]);
document.write("Orizinal TypedArray: ", T_array);
document.write("<br>First negative number in TypedArray is: ");
document.write(T_array.findIndex(isPrime));
</script>
</body>
</html>
输出
执行上述程序后,它将返回 -1。
First negative number in TypedArray is: -1
FindIndex() 与 Find() 方法
findIndex() 和 find() 方法彼此相似,但下面列出了一些差异 -
- 类型化数组上的 findIndex() 和 find() 方法非常相似。findIndex() 返回类型化数组中第一个元素所在的索引,而 find() 方法返回第一个元素本身。
- 如果未满足任何值或没有元素传递给 findIndex() 方法,则返回 −1,而 find() 方法返回 'undefined'。