简体   繁体   English

Map-Reduce:如何在集合中计数

[英]Map-Reduce : How to count in a collection

Important edit : I can't use filter - the purpose is pedagogic. 重要编辑: 我不能使用filter -目的是教学目的。

I have an array in which I would want to count the number of its elements that verify a boolean, using only map and reduce . 我有一个数组,我想只使用mapreduce来计算其验证布尔值的元素的数量。

Count of the array's size 数组大小的计数

I already wrote something that counts the array's size (ie. : the number of all of its elements), using reduce : 我已经使用reduce编写了一些计算数组大小(即: 所有元素的数量)的东西:

const array_numbers = [12, 15, 1, 1]; // Size : 4
console.log(
              array_numbers.reduce((acc) => {
                return acc + 1;
              }, 0)
);

Count of the array's elements checking a boolean condition 检查布尔条件的数组元素计数

Now I would want to count only the elements that verify a boolean condition. 现在,我只想计算验证布尔条件的元素。 Thus, I must use map before reduce and the elements of the map 's returned array will be only the good elements. 因此,我必须在reduce之前使用map ,并且map的返回数组的元素将只是好元素。

So I wrote this code but it doesn't work... Indeed, I put null when I encounter a not-good element (and null is counted as en element unfortunately). 因此,我编写了这段代码,但是它不起作用...实际上,当遇到一个不好的元素时,我将null设置为null (不幸的是, null被视为en元素)。

NB : here, the boolean condition is "is the element even ? (%2 == 0)". 注意:此处的布尔条件为“元素是否为偶?(%2 == 0)”。

const array_numbers = [12, 15, 1, 1]; // Size : 4
console.log(
              array_numbers.map((current_value) => {
                if(current_value % 2 == 0) {
                  return current_value;
                }
                return null;

              }).reduce((acc) => {
                return acc + 1;

              }, 0)
);

Array#filter the array and check the length: Array#filter数组并检查长度:

 const array_numbers = [12, 15, 1, 1]; const result = array_numbers.filter((n) => n % 2 === 0).length; console.log(result); 

Or count using Array#reduce : 或使用Array#reduce计数:

 const array_numbers = [12, 15, 1, 1, 4]; const result = array_numbers.reduce((r, n) => n % 2 ? r : r + 1, 0); console.log(result); 

Or if you must, you can use Array#map with Array#reduce : 或者,如果必须,您可以将Array#mapArray#reduce

 const array_numbers = [12, 15, 1, 1, 4]; const result = array_numbers .map((n) => n % 2 === 0 ? 1 : 0) // map to 1 or 0 according to the condition .reduce((r, n) => r + n); // sum everything console.log(result); 

You can use Array.prototype.filter to filter the even numbers - and you don't need the reduce() function - you can use length of the array returned by the filter() function. 您可以使用Array.prototype.filter过滤偶数 -不需要reduce()函数-您可以使用filter()函数返回的数组length

Or you can use reduce() method alone like below: 或者您可以单独使用reduce()方法,如下所示:

See demos below: 请参见下面的演示:

 const array_numbers = [12, 15, 1, 1]; // Size : 4 // using filter console.log( array_numbers.filter((current_value) => { return current_value % 2 == 0; }).length ); // using reduce console.log( array_numbers.reduce((prev, curr) => { return curr % 2 == 0 ? prev + 1 : prev; }, 0) ); 

Since per your comment you must use reduce for some reason: 由于您的评论,出于某种原因,您必须使用reduce:

arr.reduce((acc, n) => { n % 2 ? acc + n : acc }, 0);

The map is unecessary. 该地图是不必要的。

As Jared Smith mentioned, you don't need to use map for this task. 正如Jared Smith提到的那样,您无需使用map完成此任务。 Array.reduce() gets the current element as a second argument in the callback function which you can use to check if it satisfies your given condition. Array.reduce()将当前元素作为回调函数中的第二个参数,您可以使用该函数检查它是否满足给定条件。

So, again assuming that you must use either map and/or reduce : 因此,再次假设您必须使用map和/或reduce

 const myArray = [1,2,3,4,5,6]; const condition = function(a) { // let's say return a %2 == 0; } let result = myArray.reduce((acc, val) => { return condition(val) ? acc + 1 : acc }, 0); console.log(result); 

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

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