#python:
def countEntries(array, predicate):
return sum(1 for v in array if predicate(v))
#javascript:
function countEntries(array, predicate) {
let c = 0;
for (const v of array) {
if (predicate(v)) {
++c;
}
}
return c;
}
Better?
-
Show this thread
-
Array.reduce works well for this: const countEntries = (array, predicate) => ( array.reduce((a, v) => (predicate(v) ? a + 1 : a), 0));
1 reply 0 retweets 0 likesShow this thread
Replying to @halcanary
const countEntries = (array, predicate) => array.filter(predicate).length
4:51 AM - 19 Jun 2020
0 replies
0 retweets
1 like
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.