Skip to content

Instantly share code, notes, and snippets.

@conradfr
Created August 6, 2014 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conradfr/ce078d44992fd1011c3c to your computer and use it in GitHub Desktop.
Save conradfr/ce078d44992fd1011c3c to your computer and use it in GitHub Desktop.
filterByValues underscore.js mixin
/**
* Filter a collection by a property and an array of values
* demo: http://jsfiddle.net/3Y95F/
*
* @param {array|object} collection (of objects)
* @param {string} property
* @param {array} array of accepted values
* @returns {array|object}
*/
_.mixin({
'filterByValues': function(collection, property, values) {
return _.filter(collection, function(item) {
return _.contains(values, item[property]);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment