简体   繁体   English

过滤数组并将其转换为2D数组

[英]Filtering an array and converting it to a 2D array

I saw this interesting post yesterday, and thought it'd be important to know how to create a 2D array from sorting the given argument: How to get even numbers array to print first instead of odds? 昨天我看到了这篇有趣的文章,并认为了解如何通过对给定参数进行排序来创建2D数组非常重要: 如何使偶数数组首先打印而不是赔率?

Below is the code snippet from Ori Drori. 以下是Ori Drori的代码段。

I was curious to know what line of code, and which expression sorts the data and creates 2D array. 我很想知道哪一行代码,以及哪个表达式对数据进行排序并创建2D数组。 I assume it's something to do with [numbersArray[i] % 2], but isn't the remainder operator returns the remainder left over? 我认为这与[numbersArray [i]%2]有关,但是余数运算符不是返回剩余的余数吗?

Also it's a bit confusing as it just set one bracket for an array and use push() to make 2 different arrays. 同样,这有点令人困惑,因为它只是为一个数组设置了一个括号并使用push()来创建2个不同的数组。

Any reference that'd help me to understand this will also be much appreciated- thanks! 任何可以帮助我理解这一点的参考资料也将不胜感激-谢谢!

 var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13]; function divider(numbersArray) { var evensOdds = [[], []]; for (var i = 0; i < numbersArray.length; i++) { evensOdds[numbersArray[i] % 2].push(numbersArray[i]); } return evensOdds; } console.log(divider(numbersArray)); 

evensOdds has 2 array elements. evensOdds具有2个数组元素。 evensOdds[0] represents first array, which will hold even nums. evensOdds[0]表示第一个数组,该数组将保留偶数。 evensOdds[1] is second element and will hold odd numbers. evensOdds[1]是第二个元素,将保留奇数。

When you % 2 an even number, it will result in 0 and 1 for odd number. 当您% 2个偶数时,它将导致01为奇数。 So when iterating through the array, you % 2 , which will return 0 or 1 which enables you to access the first or second array in your evensOdds array and insert it. 因此,当遍历数组时,您将% 2 ,将返回01 ,这使您可以访问evensOdds数组中的第一个或第二个数组并将其插入。

The resulting arrays are not sorted but does represent the split between even and odd numbers. 结果数组未排序,但确实代表了偶数和奇数之间的分隔。 To have a sorted results, you will need the following: 要获得排序结果,您将需要以下内容:

 var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13]; function divider(numbersArray) { var evensOdds = [[], []]; for (var i = 0; i < numbersArray.length; i++) { evensOdds[numbersArray[i] % 2].push(numbersArray[i]); } return evensOdds.map(array=> array.sort((a,b)=>ab)); } console.log(divider(numbersArray)); 

In the shared code the evensOdds[numbersArray[i] % 2] line is the part that filter the numbers and inserts them in the respective array , using the indexes 0 and 1 returned from the numbersArray[i] % 2 expression: 在共享代码中, evensOdds[numbersArray[i] % 2]行是过滤数字并将其使用从numbersArray[i] % 2表达式返回的索引01将其插入相应array的部分:

If it returns 0 so it's an even number and it will be pushed in the first array otherwise if it returns 1 it's an odd number and will be pushed in the second array. 如果返回0 ,则为even ,将被压入第一个array否则,返回1odd ,并将被压入第二个数组。

Another alternative: 另一种选择:

Well you can simply use Array.filter() method to filter both evens and odds arrays: 那么你可以简单地使用Array.filter()方法来过滤都evensodds阵列:

Demo: 演示:

 var numbersArray = [1, 2, 34, 54, 55, 34, 32, 11, 19, 17, 54, 66, 13]; var evens = numbersArray.filter(function(el) { return el % 2 == 0; }); var odds = numbersArray.filter(function(el) { return el % 2 == 1; }); console.log(evens); console.log(odds); 

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

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