Created
February 17, 2018 11:05
-
-
Save edwardlorilla/cb399fdfe56d646171f24697c5f0e8a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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