_.partition

_.partition(collection, [predicate=_.identity])

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

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the array of grouped elements.

Example

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']]
doc_Lodash
2016-11-27 16:39:34
Comments
Leave a Comment

Please login to continue.