Using _.partition in lodash to break a collection into two groups

In lodash there is a method that can be used to break a collection into two groups one of which meets a condition that is given in a function that is passed to it, and another that does not meet that condition. This is of course the _.partition method.

Example of _.partition

For a quick example of this I thought it would be fun to play with an array that represents a hand of seven French playing cards. The _.partition method can be used to split the collection of objects into two separate groups that meet a given condition, such as if a card has a value greater than or equal to eleven.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var cards = [
{suit: 'clubs', val: '1', desc: 'ace'},
{suit: 'spades', val: '11', desc: 'jack'},
{suit: 'hearts', val: '5', desc: '5'},
{suit: 'hearts', val: '6', desc: '6'},
{suit: 'diamonds', val: '7', desc: '7'},
{suit: 'clubs', val: '13', desc: 'king'},
{suit: 'hearts', val: '12', desc: 'queen'},
];
_.each(_.partition(cards, function(card){
return card.val >= 11;
}), function(part){
console.log('** part: ' + i + ' **');
_.each(part, function(card){
console.log(card.desc);
});
});

In this example I am using _.partition to create an Array of Arrays the first of which is an array of cards that meets the criteria of the condition that I gave it, and the second is everything else that remains. I then used _.each to loop over each of these arrays, and then again to loop over each card.

As such I get what is expected.

1
2
3
4
5
6
7
8
9
** part: 0 **
jack
king
queen
** part: 1 **
ace
5
6
7

So as the name suggested _.partition is useful for well partitioning an array into two arrays one of which meets the conditions given, and the other does not.