Skip to content
Advertisement

ternary operator within .filter()

A simple example.

JavaScript

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)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement