Proposal: Array.prototype.count

# Засим Александр (6 years ago)

This is proposal for Array.prototype.count (or countOf) method which allow to count specific elements in an array.

const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0);

Instead of

const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 === 0).length;
# Ranando King (6 years ago)

Isn't that just a specific case of Array.prototype.reduce?

const evenNumberCount = [1,2,3,4,5].reduce((acc, val) => { !(val % 2)
&& ++acc; });
# Jordan Harband (6 years ago)

All the iteration methods are a specific case of reduce, in that you can implement them all with reduce.

# Pier Bover (6 years ago)

If you need to find the length of a filtered array IMO it makes more sense and is more obvious to just use filter().

# Ranando King (6 years ago)

Either way it goes, there's a lot of ways to do this that are all trivial.

# Andrea Giammarchi (6 years ago)

[time to write specs, pass through the whole process, ship it to all engines, have it 100% usable without polyfills]

[value of this proposal]

both sentences represents time, -1 here