We can assert some predicate over a collection using _.some
to check if there
is at least one member of a collection that meets some criteria. This is great
when writing business logic to assert certain conditions on a group of objects.
For example, let's say you wanted to make sure at least one person in a group
had a driver's license before it was possible for that group to go on a road
trip. We make no guarantees on how happy of a group that'll be at the end of
the road trip, however.
var friends = [
{
'name': 'Fred',
'hasLicense': false
},
{
'name': 'Steve',
'hasGuitar': true
},
{
'name': 'Mary',
'hasLicense': true
},
]
function canGroupDrive(arr){
return _.some(arr, function(e){ return e.hasLicense; });
}
canGroupDrive(friends); // returns true