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'。

廣告