主题
partition
js
_.partition(collection, [predicate=_.identity])
创建一个元素数组,这些元素分成两组,第一组包含 predicate
返回真值的元素,第二组包含 predicate
返回假值的元素。使用一个参数调用谓词:(value)。
¥Creates an array of elements split into two groups, the first of which contains elements predicate
returns truthy for, the second of which contains elements predicate
returns falsey for. The predicate is invoked with one argument: (value).
新增于
¥Since
3.0.0
参数
¥Arguments
collection
(数组|对象):要迭代的集合。¥
collection
(Array|Object): The collection to iterate over.[predicate=_.identity]
(函数):每次迭代调用的函数。¥
[predicate=_.identity]
(Function): The function invoked per iteration.
返回
¥Returns
(数组):返回分组元素的数组。
¥(Array): Returns the array of grouped elements.
示例
¥Example
js
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
{ 'user': 'pebbles', 'age': 1, 'active': false }
];
_.partition(users, function(o) { return o.active; });
// => objects for [['fred'], ['barney', 'pebbles']]
// The `_.matches` iteratee shorthand.
_.partition(users, { 'age': 1, 'active': false });
// => objects for [['pebbles'], ['barney', 'fred']]
// The `_.matchesProperty` iteratee shorthand.
_.partition(users, ['active', false]);
// => objects for [['barney', 'pebbles'], ['fred']]
// The `_.property` iteratee shorthand.
_.partition(users, 'active');
// => objects for [['fred'], ['barney', 'pebbles']]