JavaScript - TypedArray includes() 方法



JavaScript TypedArray 的includes() 方法用於確定搜尋元素值是否存在於整個 TypedArray 中,或者是否存在於從 fromIndex(起始位置)到 TypedArray 末尾的範圍內。此方法返回一個布林值'true',如果在 TypedArray 中找到指定的 searchElement;如果未找到 searchElement,則返回'false'

語法

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

includes(searchElement, fromIndex)

引數

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

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

  • fromIndex - 從零開始的索引,從此處開始搜尋 searchElement。

返回值

如果在 TypedArray 中找到 searchElement,則此方法返回“true”;否則返回“false”。

示例

示例 1

如果在 TypedArray 中未找到指定的searchElement,則將返回'false'

在以下示例中,我們使用 JavaScript TypedArray includes() 方法來確定指定的 searchElement 20 是否在此 TypedArray [10, 40, 30, 50, 80, 100] 中找到。

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 40, 30, 50, 80, 100]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 20;
      document.write("<br>The elenent to be searched: ", searchElement);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found in typed array ? ",  T_array.includes(searchElement));
   </script>    
</body>
</html>

輸出

上述程式返回“false”。

The typed array elements are: 10,40,30,50,80,100
The elenent to be searched: 20
Does element 20 is found in typed array ? false

示例 2

如果在 TypedArray 中找到指定的searchElement,則將返回'true'

以下是 JavaScript TypedArray includes() 方法的另一個示例。我們使用此方法來確定指定的 searchElement 4 是否在此 TypedArray [1, 2, 3, 4, 5, 6, 7, 8] 中找到。

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 4;
      document.write("<br>The elenent to be searched: ", searchElement);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found in typed array ? ",  T_array.includes(searchElement));
   </script>    
</body>
</html>

輸出

執行上述程式後,它將返回“true”。

The typed array elements are: 1,2,3,4,5,6,7,8
The elenent to be searched: 4
Does element 4 is found in typed array ? true

示例 3

如果將'searchElement''fromIndex' 兩個引數都傳遞給此方法,它將從 TypedArray 中指定的 'fromIndex' 開始搜尋 'searchElement'。

在此程式中,我們使用includes() 方法來確定指定的 searchElement 2 是否在此 TypedArray [1, 2, 3, 5, 7, 9, 6, 8, 11, 15] 的起始位置4 到陣列末尾之間找到。

<html>
<head>
   <title>JavaScript TypedArray includes() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 5, 7, 9, 6, 8, 11, 15]);
      document.write("The typed array elements are: ", T_array);
      const searchElement = 2;
      const fromIndex = 3;
      document.write("<br>The elenent to be searched: ", searchElement);
      document.write("<br>The starting position: ", fromIndex);
      
      //using the includes() method
      document.write("<br>Does element ", searchElement, " is found between start index ", 
      fromIndex, " and till the end of typed array ? ",  T_array.includes(searchElement, fromIndex));
   </script>    
</body>
</html>

輸出

執行上述程式後,它將返回“false”。

The typed array elements are: 1,2,3,5,7,9,6,8,11,15
The elenent to be searched: 2
The starting position: 3
Does element 2 is found between start index 3 and till the end of typed array ? false
廣告