Skip to content

Instantly share code, notes, and snippets.

@edwardlorilla
Created February 17, 2018 11:05
Show Gist options
  • Save edwardlorilla/cb399fdfe56d646171f24697c5f0e8a3 to your computer and use it in GitHub Desktop.
Save edwardlorilla/cb399fdfe56d646171f24697c5f0e8a3 to your computer and use it in GitHub Desktop.
function multiFilter(array, filters) {
var filterKeys = Object.keys(filters);
return array.filter(function (eachObj) {
return filterKeys.every(function (eachKey) {
if (!filters[eachKey].length) {
return true; // passing an empty filter means that filter is ignored.
}
return _.isArray(eachObj[eachKey]) ?
eachObj[eachKey].some(function (o) {
return filters[eachKey].includes(o.name);
})
: filters[eachKey].includes(_.isObject(eachObj[eachKey]) ? eachObj[eachKey].name : eachObj[eachKey])
});
});
}
//array {array}
//filters {object}
//usage var filtered = multiFilter(array, filters);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment