Array.prototype.every()

Array 实例的 every() 方法测试数组中的所有元素是否通过提供的函数实现的测试。它返回一个布尔值。

¥The every() method of Array instances tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

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 an array element, in which case false is immediately returned.

描述

¥Description

every() 方法是 迭代法 方法。它为数组中的每个元素调用一次提供的 callbackFn 函数,直到 callbackFn 返回 falsy 值。如果找到这样的元素,every() 立即返回 false 并停止遍历数组。否则,如果 callbackFn 对所有元素返回 truthy 值,则 every() 返回 true。请阅读 迭代法 部分,了解有关这些方法一般如何工作的更多信息。

¥The every() method is an iterative method. It calls a provided callbackFn function once for each element in an array, until the callbackFn returns a falsy value. If such an element is found, every() immediately returns false and stops iterating through the array. Otherwise, if callbackFn returns a truthy value for all elements, every() returns true. Read the iterative methods section for more information about how these methods work in general.

every 的作用类似于数学中的 "对全部" 量词。特别是,对于空数组,它返回 true。(空集 的所有元素满足任意给定条件就是 空洞真实。)

¥every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true. (It is vacuously true that all elements of the empty set satisfy any given condition.)

callbackFn 仅针对已赋值的数组索引调用。稀疏数组 中的空槽不会调用它。

¥callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.

every() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。

¥The every() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

测试所有数组元素的大小

¥Testing size of all array elements

以下示例测试数组中的所有元素是否为 10 或更大。

¥The following example tests whether all elements in the array are 10 or bigger.

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

检查一个数组是否是另一个数组的子集

¥Check if one array is a subset of another array

以下示例测试一个数组的所有元素是否都存在于另一个数组中。

¥The following example tests if all the elements of an array are present in another array.

js
const isSubset = (array1, array2) =>
  array2.every((element) => array1.includes(element));

console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 7, 6])); // true
console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7])); // false

使用 callbackFn 的第三个参数

¥Using the third argument of callbackFn

如果你想访问数组中的另一个元素,则 array 参数很有用。以下示例首先使用 filter() 提取正值,然后使用 every() 检查数组是否严格递增。

¥The array argument is useful if you want to access another element in the array. The following example first uses filter() to extract the positive values and then uses every() to check whether the array is strictly increasing.

js
const numbers = [-2, 4, -8, 16, -32];
const isIncreasing = numbers
  .filter((num) => num > 0)
  .every((num, idx, arr) => {
    // Without the arr argument, there's no way to easily access the
    // intermediate array without saving it to a variable.
    if (idx === 0) return true;
    return num > arr[idx - 1];
  });
console.log(isIncreasing); // true

在稀疏数组上使用 every()

¥Using every() on sparse arrays

every() 不会在空槽上运行其谓词。

¥every() will not run its predicate on empty slots.

js
console.log([1, , 3].every((x) => x !== undefined)); // true
console.log([2, , 2].every((x) => x === 2)); // true

对非数组对象调用 every()

¥Calling every() on non-array objects

every() 方法读取 thislength 属性,然后使用小于 length 的非负整数键访问每个属性,直到它们全部被访问或 callbackFn 返回 false

¥The every() method reads the length property of this and then accesses each property with a nonnegative integer key less than length until they all have been accessed or callbackFn returns false.

js
const arrayLike = {
  length: 3,
  0: "a",
  1: "b",
  2: "c",
  3: 345, // ignored by every() since length is 3
};
console.log(
  Array.prototype.every.call(arrayLike, (x) => typeof x === "string"),
); // true

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.every

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看