TypedArray.prototype.some()

TypedArray 实例的 some() 方法测试类型化数组中的至少一个元素是否通过所提供函数实现的测试。如果在类型化数组中找到所提供的函数返回 true 的元素,则返回 true;否则返回 false。它不会修改类型化数组。该方法与 Array.prototype.some() 的算法相同。

¥The some() method of TypedArray instances tests whether at least one element in the typed array passes the test implemented by the provided function. It returns true if, in the typed array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the typed array. This method has the same algorithm as Array.prototype.some().

Try it

语法

¥Syntax

js
some(callbackFn)
some(callbackFn, thisArg)

参数

¥Parameters

callbackFn

对类型化数组中的每个元素执行的函数。它应该返回 truthy 值以指示元素通过测试,否则返回 falsy 值。使用以下参数调用该函数:

element

类型化数组中正在处理的当前元素。

index

类型化数组中当前正在处理的元素的索引。

array

调用了类型化数组 some()

thisArg Optional

执行 callbackFn 时用作 this 的值。参见 迭代法

返回值

¥Return value

false,除非 callbackFn 返回类型化数组元素的 truthy 值,在这种情况下,立即返回 true

¥false unless callbackFn returns a truthy value for a typed array element, in which case true is immediately returned.

描述

¥Description

详细信息请参见 Array.prototype.some()。此方法不是通用的,只能在类型化数组实例上调用。

¥See Array.prototype.some() for more details. This method is not generic and can only be called on typed array instances.

示例

¥Examples

测试所有类型数组元素的大小

¥Testing size of all typed array elements

以下示例测试类型化数组中的任何元素是否大于 10。

¥The following example tests whether any element in the typed array is bigger than 10.

js
function isBiggerThan10(element, index, array) {
  return element > 10;
}
new Uint8Array([2, 5, 8, 1, 4]).some(isBiggerThan10); // false
new Uint8Array([12, 5, 8, 1, 4]).some(isBiggerThan10); // true

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.some

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看