A simple example.
var evens= true; var numbers = [1,2,3,4,5] var result = []; if(evens){ result = numbers.filter(num => num%2 === 0) } else { result = numbers.filter(num => num%2 !== 0) }
Is there a way to get something like result = numbers.filter(num => num%2 (evens) ? === : !== 0)
where depending on ‘evens’ to use equals or not equals to 0 within the .filter function? Is there a way to reuse same function to not have replication?
Advertisement
Answer
How about just
result = numbers.filter(num => !(num % 2) === evens)