Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/11093988 to your computer and use it in GitHub Desktop.
Save pgilad/11093988 to your computer and use it in GitHub Desktop.
Check if a collection has truthy values for keys
//to validate if a collection has all the desired keys and they are truthy:
var hasEvery = function(desiredKeys, collection) {
return _.all(desiredKeys, _.result.bind(collection, collection));
};
var desiredKeys = ['hello', 'there', 'isIt'];
var collection = { hello: 1, there: 1, isIt: true};
hasEvery(desiredKeys, collection);
//-> true
collection.isIt = false;
hasEvery(desiredKeys, collection);
//-> false
desiredKeys = ['hello', 'me'];
hasEvery(desiredKeys, collection);
//-> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment