JavaScript TypedArray includes() 方法用于确定 searchElement 值是存在于整个类型化数组中,还是存在于从 fromIndex(起始位置)到类型化数组末尾的范围内。如果在类型化数组中找到指定的 searchElement,则此方法返回布尔值 'true'。如果未找到 searchElement,则返回 'false'。
语法
以下是 JavaScript TypedArray includes() 方法的语法 -
参数
此方法接受两个名为 'searchElement' 和 'fromIndex' 的参数,如下所述 -
- searchElement − 要在类型化数组中搜索的元素。
- fromIndex − 从零开始搜索 searchElement 的索引。
返回值
如果在类型化数组中找到 searchElement,则此方法返回 'true';否则为 'false'。
示例 1
如果在类型化数组中找不到指定的 searchElement,它将返回 'false'。
在下面的示例中,我们使用 JavaScript TypedArray includes() 方法来确定是否在此类型化数组 [10, 40, 30, 50, 80, 100] 中找到指定的 searchElement 20。
输出
上述程序返回 'false'。
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。
输出
执行上述程序后,返回 'true'。
The elenent to be searched: 4
Does element 4 is found in typed array ? true
示例 3
如果 'searchElement' 和 'fromIndex' 参数都传递给此方法,它将开始从类型化数组中的指定 'fromIndex' 中搜索 'searchElement'。
在这个程序中,我们使用 includes() 方法来确定是否在起始位置 4 之间找到指定的 searchElement 2,直到这个类型化数组的结尾 [1, 2, 3, 5, 7, 9, 6, 8, 11, 15]。
输出
执行上述程序后,它将返回 'false'。
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