JavaScript - TypedArray includes() 方法
JavaScript TypedArray 的 includes() 方法用于判断 searchElement 值是否存在于整个类型数组中,或者是否存在于从 fromIndex(起始位置)到类型数组末尾的范围内。如果在类型数组中找到指定的 searchElement,则此方法返回布尔值 'true'。如果未找到 searchElement,则返回 'false'。
语法
以下是 JavaScript TypedArray includes() 方法的语法 -
includes(searchElement, fromIndex)
参数
此方法接受两个名为"searchElement"和"fromIndex"的参数,如下所述 -
searchElement - 要在类型数组中搜索的元素。
fromIndex - 从零开始搜索 searchElement 的索引。
返回值
如果如果在类型化数组中找到 searchElement,则返回"false"。
示例
示例 1
如果在类型化数组中未找到指定的 searchElement,则返回 'false'。
在下面的示例中,我们使用 JavaScript TypedArray 的 includes() 方法判断是否在类型化数组 [10, 40, 30, 50, 80, 100] 中找到指定的 searchElement 20。
<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);
//使用 includes() 方法
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
如果在类型化数组中找到指定的 searchElement,则返回 'true'。
以下是 JavaScript TypedArray includes() 方法的另一个示例。我们使用此方法判断是否在类型化数组 [1, 2, 3, 4, 5, 6, 7, 8] 中找到指定的 searchElement 4。
<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);
//使用 includes() 方法
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' 参数,则它会从类型数组中指定的 'fromIndex' 开始搜索 'searchElement'。
在此程序中,我们使用 includes() 方法判断指定的 searchElement 2 是否在起始位置 4 和此类型数组的末尾 [1, 2, 3, 5, 7, 9, 6, 8, 11, 15] 之间找到。
<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);
//使用 includes() 方法
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

