Proposal: Array.prototype.count
# Ranando King (7 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; });
Isn't that just a specific case of Array.prototype.reduce?
```js
const evenNumberCount = [1,2,3,4,5].reduce((acc, val) => { !(val % 2)
&& ++acc; });
```
On Mon, Jan 7, 2019 at 1:12 PM Засим Александр <al.zasim at gmail.com> wrote:
> Hi everyone. This is proposal for Array.prototype.count (or countOf)
> method which allow to count specific elements in an array.
>
> ```js
>
> const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0);
>
> ```
>
> Instead of
>
> ```js
>
> const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 ===
> 0).length;
>
> ```
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190107/49754f50/attachment.html># Jordan Harband (7 years ago)
All the iteration methods are a specific case of reduce, in that you can implement them all with reduce.
All the iteration methods are a specific case of reduce, in that you can
implement them all with reduce.
On Mon, Jan 7, 2019 at 11:27 AM Ranando King <kingmph at gmail.com> wrote:
> Isn't that just a specific case of Array.prototype.reduce?
>
> ```js
> const evenNumberCount = [1,2,3,4,5].reduce((acc, val) => { !(val % 2)
> && ++acc; });
> ```
>
> On Mon, Jan 7, 2019 at 1:12 PM Засим Александр <al.zasim at gmail.com> wrote:
>
>> Hi everyone. This is proposal for Array.prototype.count (or countOf)
>> method which allow to count specific elements in an array.
>>
>> ```js
>>
>> const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0);
>>
>> ```
>>
>> Instead of
>>
>> ```js
>>
>> const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 ===
>> 0).length;
>>
>> ```
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190107/0e5edd64/attachment.html># Pier Bover (7 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().
If you need to find the length of a filtered array IMO it makes more sense and is more obvious to just use filter(). On Mon, Jan 7, 2019 at 1:12 PM Засим Александр <al.zasim at gmail.com> wrote: > Hi everyone. This is proposal for Array.prototype.count (or countOf) > method which allow to count specific elements in an array. > > ```js > > const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0); > > ``` > > Instead of > > ```js > > const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 === > 0).length; > > ``` > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190107/c8b792f9/attachment-0001.html>
# Ranando King (7 years ago)
Either way it goes, there's a lot of ways to do this that are all trivial.
Either way it goes, there's a lot of ways to do this that are all trivial. On Mon, Jan 7, 2019 at 1:31 PM Pier Bover <pierbover11 at gmail.com> wrote: > If you need to find the length of a filtered array IMO it makes more sense > and is more obvious to just use filter(). > > On Mon, Jan 7, 2019 at 1:12 PM Засим Александр <al.zasim at gmail.com> wrote: > >> Hi everyone. This is proposal for Array.prototype.count (or countOf) >> method which allow to count specific elements in an array. >> >> ```js >> >> const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0); >> >> ``` >> >> Instead of >> >> ```js >> >> const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 === >> 0).length; >> >> ``` >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190107/9f361c32/attachment.html>
# Andrea Giammarchi (7 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
[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 On Mon, Jan 7, 2019 at 8:35 PM Ranando King <kingmph at gmail.com> wrote: > Either way it goes, there's a lot of ways to do this that are all trivial. > > On Mon, Jan 7, 2019 at 1:31 PM Pier Bover <pierbover11 at gmail.com> wrote: > >> If you need to find the length of a filtered array IMO it makes more >> sense and is more obvious to just use filter(). >> >> On Mon, Jan 7, 2019 at 1:12 PM Засим Александр <al.zasim at gmail.com> >> wrote: >> >>> Hi everyone. This is proposal for Array.prototype.count (or countOf) >>> method which allow to count specific elements in an array. >>> >>> ```js >>> >>> const evenNumberCount = [1, 2, 3, 4, 5].count(num => num % 2 === 0); >>> >>> ``` >>> >>> Instead of >>> >>> ```js >>> >>> const evenNumberCount = [1, 2, 3, 4, 5].filter(num => num % 2 === >>> 0).length; >>> >>> ``` >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190107/919b6e7d/attachment.html>
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;