Skip to content

Instantly share code, notes, and snippets.

@scinetic
Last active February 2, 2021 22:37
Show Gist options
  • Save scinetic/6c0906f0ce8bae27930f372150f6becd to your computer and use it in GitHub Desktop.
Save scinetic/6c0906f0ce8bae27930f372150f6becd to your computer and use it in GitHub Desktop.
Combine multiple filters with Javascript
const arr = [1, 2, 3, 4, 5];
const upperBoundaryCheck = (value) => {
if (value < 10) {
return true;
}
return false;
};
const lowerBoundaryCheck = (value) => {
if (value > 1) {
return true;
}
return false;
};
const combineFilters = (...filters) => (item) => {
return filters.map((filter) => filter(item)).every((x) => x === true);
};
const combinedFilters = combineFilters(upperBoundaryCheck, lowerBoundaryCheck);
console.log(arr.filter(combinedFilters));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment