Created
September 24, 2020 11:53
-
-
Save 0xlkda/fb77b5664c3030551c6140ccbb67dc6f to your computer and use it in GitHub Desktop.
Filter by list of conditions
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 filterBy = (conditions, index = 0) => (value) => { | |
if(conditions[index] === undefined) return value | |
if(!conditions[index](value)) return | |
return filterBy(conditions, index + 1)(value) | |
} | |
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 25, 27] | |
const odd = (value) => value % 2 !== 0 | |
const greaterThan3 = (value) => value > 3 | |
const canDivide3 = (value) => value % 3 === 0 | |
const conditions = [odd, greaterThan3, canDivide3] | |
const results = values.map(filterBy(conditions)).filter(x => x) | |
console.log(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment