JavaScript - TypedArray lastIndexOf() 方法



JavaScript TypedArray 的 lastIndexOf() 方法從右到左在 TypedArray 中搜索指定的 searchElement。如果找到,則返回 searchElement 最後一次出現的索引;如果未找到,則返回 -1

假設指定的 searchElement 在 TypedArray 中只出現一次,則此方法將返回從第零個索引開始的索引。如果 searchElement 出現多次,它將優先考慮最後一個元素。

語法

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

lastIndexOf(searchElement, fromIndex)

引數

此方法接受兩個引數,即 'searchElement' 和 'fromIndex'。引數描述如下:

  • searchElement - 需要在 TypedArray 中搜索的元素。

  • fromIndex - 基於零的索引,指示從末尾開始搜尋的位置。

返回值

此方法返回 searchElement 在 TypedArray 中的最後一個索引。如果未找到 searchElement,則返回 -1。

示例

示例 1

如果省略 fromIndex 引數,只傳遞 searchElement 引數,則該方法將從 TypedArray 的末尾返回指定 searchElement 的最後一個索引。

在下面的示例中,我們使用 JavaScript TypedArray 方法 lastIndexOf() 來檢索指定 searchElement 30 在 TypedArray [10, 20, 30, 40, 30, 50, 60] 中的最後一個索引。

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 30, 40, 50, 60]);
      document.write("Original typed array: ", T_array);
      const serachElement = 30;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement));
   </script>    
</body>
</html>

輸出

上述程式返回元素 30 最後一次出現的索引為 4。

Original typed array: 10,20,30,30,40,50,60
Search element: 30
The element 30 found at the last index 3

示例 2

如果我們將 'searchElement''fromIndex' 引數都傳遞給此方法,它將返回從指定的 fromIndex 開始的元素的最後一個索引。

以下是 JavaScript TypedArray lastIndexOf() 方法的另一個示例。我們使用此方法來檢索指定元素 6 的最後一個索引,從索引 5 開始搜尋,在這個 TypedArray 中:[1, 2, 4, 6, 8, 9, 6, 11]。

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2 , 4, 6, 8, 9, 6, 11]);
      document.write("Original typed array: ", T_array);
      const serachElement = 6;
      const fromIndex = 5;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>From index: ", fromIndex);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement, fromIndex));
   </script>    
</body>
</html>

輸出

執行程式後,將返回元素 6 最後一次出現的索引為 6。

Original typed array: 1,2,4,6,8,9,6,11
Search element: 6
From index: 5
The element 6 found at the last index 3

示例 3

如果在此 TypedArray 中找不到指定的 searchElement,則返回 -1

在此示例中,我們對 TypedArray 使用 TypedArray lastIndexOf() 方法來檢索 searchElement 11 在給定的 TypedArray [1, 3, 5, 7, 13] 中的最後一個索引。

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array( [1, 3, 5, 7, 13]);
      document.write("Original typed array: ", T_array);
      const serachElement = 6;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement));
   </script>    
</body>
</html>

輸出

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

Original typed array: 1,3,5,7,13
Search element: 6
The element 6 found at the last index -1
廣告