TypedArray.prototype.every()

TypedArray 实例的 every() 方法测试类型化数组中的所有元素是否通过所提供函数实现的测试。它返回一个布尔值。该方法与 Array.prototype.every() 的算法相同。

¥The every() method of TypedArray instances tests whether all elements in the typed array pass the test implemented by the provided function. It returns a Boolean value. This method has the same algorithm as Array.prototype.every().

Try it

语法

¥Syntax

js
every(callbackFn)
every(callbackFn, thisArg)

参数

¥Parameters

callbackFn

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

element

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

index

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

array

调用了类型化数组 every()

thisArg Optional

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

返回值

¥Return value

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

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

描述

¥Description

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

¥See Array.prototype.every() 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 all elements in the typed array are 10 or bigger.

js
function isBigEnough(element, index, array) {
  return element >= 10;
}
new Uint8Array([12, 5, 8, 130, 44]).every(isBigEnough); // false
new Uint8Array([12, 54, 18, 130, 44]).every(isBigEnough); // true

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看