Anyul Rivas Anyul Rivas - 6 months ago 26
Javascript Question

Set an array of elements not in another array

I have an array of numbers

const numbers = [12,37,5,42,8,3];

And i want to set an array of even numbers from my initial array

const even = numbers.filter((number) => {return number % 2==0; });

I new a method to get the odd numbers by extracting the even numbers out of the initial array.

or even better, what's the optimal way to the two array with less computation/iterations?


With one iteration:

const {even, odd} = numbers.reduce((a, b) => (a[b % 2 === 0 ? 'even' : 'odd'].push(b), a) , {even: [], odd: []});