JavaScript - TypedArray indexOf() 方法



JavaScript TypedArray 的indexOf()方法用於檢索指定元素首次出現或所在的位置的索引。如果指定元素在型別化陣列中重複出現兩次,則該方法將優先考慮第一次出現並返回其索引,而不是第二次出現。

如果我們將 fromIndex 引數值指定給此方法,它將從指定的 fromIndex 開始搜尋元素。如果在 fromIndex 和型別化陣列的最後一個索引之間找不到該元素,則返回−1,即使該元素存在於型別化陣列中。

語法

以下是 JavaScript TypedArray indexOf() 方法的語法:

indexOf(searchElement, fromIndex)

引數

此方法接受兩個名為“searchElement”和“fromIndex”的引數。如下所述:

  • searchElement - 要搜尋的元素。

  • fromIndex - 從其開始搜尋的基於零的索引(位置)。

返回值

此方法返回 searchElement 的第一個索引。

示例

示例 1

如果我們省略fromIndex引數,只傳遞searchElement引數給此方法,它將在整個型別化陣列中搜索此元素,並返回找到 searchElement 的第一個索引。

在下面的程式中,我們使用 JavaScript TypedArray 的indexOf()方法來檢索此型別化陣列 [1, 2, 3, 4, 5, 6, 7, 8] 中指定元素2的第一個索引。

<html>
<head>
   <title>JavaScript TypedArray indexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
      document.write("Original TypedArray: ", T_array);
      const searchElement = 3;
      document.write("<br>searchElement: ", searchElement);
      document.write("<br>The element ", searchElement, " is found at position ", T_array.indexOf(searchElement)); 
   </script>    
</body>
</html>

輸出

上述程式返回第一個索引為“2”。

Original TypedArray: 1,2,3,4,5,6,7,8
searchElement: 3
The element 3 is found at position 2

示例 2

如果我們將searchElementfromIndex引數都傳遞給此方法,它將從指定的 fromIndex 位置開始搜尋指定的 searchElement。

在此 JavaScript TypedArray indexOf() 方法的示例中,我們嘗試檢索指定元素60的第一個索引,從此型別化陣列中指定的 fromIndex 位置4開始搜尋:[10, 20, 30, 40, 50, 60, 70, 80, 60]。

<html>
<head>
   <title>JavaScript TypedArray indexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 40, 50, 60, 70, 80, 60]);
      document.write("Original TypedArray: ", T_array);
      const searchElement = 60;
      const fromIndex = 4;
      document.write("<br>searchElement: ", searchElement);
      document.write("<br>fromIndex: ", fromIndex);
      document.write("<br>The element ", searchElement, " is found at position ", T_array.indexOf(searchElement, fromIndex));
   </script>    
</body>
</html>

輸出

執行上述程式後,它將返回第一個索引為 5。

Original TypedArray: 10,20,30,40,50,60,70,80,60
searchElement: 60
fromIndex: 4
The element 60 is found at position 5

示例 3

如果型別化陣列中不存在指定的元素,則indexOf()方法將返回-1

在此示例中,我們在型別化陣列上使用indexOf()方法來檢索元素50的第一個索引。但是,此元素在 fromIndex 4 和型別化陣列 [10, 20, 50, 30, 90, 70] 的最後一個索引之間不存在。

<html>
<head>
   <title>JavaScript TypedArray indexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 50, 30, 90, 70]);
      document.write("Original TypedArray: ", T_array);
      const searchElement = 50;
      const fromIndex = 4;
      document.write("<br>searchElement: ", searchElement);
      document.write("<br>fromIndex: ", fromIndex);
      document.write("<br>The element ", searchElement, " is found at position ", T_array.indexOf(searchElement, fromIndex));
   </script>    
</body>
</html>

輸出

執行上述程式後,它將返回 -1。

Original TypedArray: 10,20,50,30,90,70
searchElement: 50
fromIndex: 4
The element 50 is found at position -1
廣告