[英]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
. 我有一个数组,我想只使用
map
和reduce
来计算其验证布尔值的元素的数量。
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)
);
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#map
与Array#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.