简体   繁体   English

设置不在另一个数组中的元素数组

[英]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? 甚至更好,用更少的计算/迭代次数到达两个数组的最佳方法是什么?

一次迭代:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM