count
函数用于计算数组中满足条件的元素个数。
Smoother.count(array, predicate)
array
:要操作的数组。
predicate
:回调函数,对数组的每个元素进行一次调用。
true
时,表示满足条件。false
时,表示不满足条件。返回一个数字,表示满足条件的元素个数。
const arr = [1, 2, 3, 4, 5];
const count = Smoother.count(arr, (element) => {
return element > 2;
});
console.log(count); // 输出:3
在上面的例子中,计算了数组 [1, 2, 3, 4, 5]
中大于 2
的元素个数,结果为 3
。