Skip to content

every

js
_.every(collection, [predicate=_.identity])

检查 predicate 是否对 collection 的所有元素返回真值。一旦 predicate 返回 false,迭代就会停止。使用三个参数调用谓词:(value, index|key, collection)。

¥Checks if predicate returns truthy for all elements of collection. Iteration is stopped once predicate returns falsey. The predicate is invoked with three arguments: (value, index|key, collection).

注意:此方法为 空集合 返回 true,因为 一切都是真的 是空集合的元素。

¥Note: This method returns true for empty collections because everything is true of elements of empty collections.

新增于

¥Since

0.1.0

参数

¥Arguments

  1. collection (数组|对象):要迭代的集合。

    ¥collection (Array|Object): The collection to iterate over.

  2. [predicate=_.identity] (函数):每次迭代调用的函数。

    ¥[predicate=_.identity] (Function): The function invoked per iteration.

返回

¥Returns

(布尔):如果所有元素都通过谓词检查,则返回 true,否则返回 false

¥(boolean): Returns true if all elements pass the predicate check, else false.

示例

¥Example

js
_.every([true, 1, null, 'yes'], Boolean);
// => false

var users = [
  { 'user': 'barney', 'age': 36, 'active': false },
  { 'user': 'fred',   'age': 40, 'active': false }
];

// The `_.matches` iteratee shorthand.
_.every(users, { 'user': 'barney', 'active': false });
// => false

// The `_.matchesProperty` iteratee shorthand.
_.every(users, ['active', false]);
// => true

// The `_.property` iteratee shorthand.
_.every(users, 'active');
// => false

Lodash v4.17 中文网 - 粤ICP备13048890号