我刚开始学习 JavaScript,遇到了一个问题,就是如何统计布尔值数组中 true 的数量。我试着使用 reduce() 函数,但好像出了些问题。能否有人告诉我哪里错了呢?
//trying to count the number of true in an array
myCount = [false,false,true,false,true].reduce(function(a,b){
return b?a++:a;
},0);
alert("myCount ="+ myCount); // this is always 0
[false,false,true,false,true].filter(x => x).length
- Andreas