Last active
February 2, 2021 22:37
-
-
Save scinetic/6c0906f0ce8bae27930f372150f6becd to your computer and use it in GitHub Desktop.
Combine multiple filters with Javascript
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
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