JavaScript - TypedArray findIndex() 方法



JavaScript TypedArray 的findIndex()方法返回当前 TypedArray 中第一个满足提供的测试函数的元素的索引。如果没有元素满足条件或没有值传递给测试函数,则返回-1

以下是关于 findIndex() 方法的一些附加说明:

  • findIndex() 方法作用于 TypedArray(例如 Uint8Array、Int16Array 等)。

  • 它接受一个测试函数作为参数。

  • 测试函数针对 TypedArray 中的每个元素执行。

  • 如果某个元素满足测试函数指定的条件(返回 true 值),则返回该元素的索引作为结果。

  • 如果测试函数没有元素满足条件,则返回-1

语法

以下是 JavaScript TypedArray findIndex() 方法的语法:

findIndex(callbackFn, thisArg)

参数

此方法接受两个参数,名为 'callbackFn' 和 'thisArg',如下所述:

callbackFn - 此参数是一个测试函数,它将对 TypedArray 中的每个元素执行。

此函数接受三个参数,名为 'element'、'index' 和 'array'。以下是每个参数的描述:

  • element - 表示 TypedArray 中当前正在处理的元素。

  • index - 指示 TypedArray 中当前元素的索引(位置)。

  • array - 指的是整个 TypedArray。

thisArg (可选) - 此参数是可选的,它允许您指定this在 callbackFn 中的值。

返回值

此方法返回 TypedArray 中第一个满足提供的测试函数的元素的索引,否则返回 '-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>

输出

上述程序在 TypedArray [1, 2, 3, 4, 5, 6, 7, 8] 中返回第一个偶数的索引 '1'。

Orizinal TypedArray: 1,2,3,4,5,6,7,8
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>

输出

执行上述程序后,它将在 TypedArray [1, 2, -1, 0, 1, 2, -2, -3] 中返回第一个负数的索引 2。

Orizinal TypedArray: 1,2,-1,0,1,2,-2,-3
An index of the first negative number in TypedArray is: 2

示例 3

如果没有值满足测试函数,它将返回'-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。

Orizinal TypedArray: 4,6,8,9
First negative number in TypedArray is: -1

findIndex() 与 find() 方法的比较

findIndex()find() 方法彼此相似,但有一些区别列在下面:

  • TypedArray 上的 findIndex() 和 find() 方法非常相似。findIndex() 返回 TypedArray 中第一个元素的索引,而 find() 方法返回第一个元素本身。

  • 如果没有值满足条件或没有元素传递给 findIndex() 方法,它将返回 -1,而 find() 方法返回 'undefined'。

广告
© . All rights reserved.