Proposal: Array.prototype.count

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: Array.prototype.count

Засим Александр

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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Array.prototype.count

Ranando King
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 Засим Александр <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Array.prototype.count

Jordan Harband
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 <[hidden email]> 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 Засим Александр <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Array.prototype.count

Pier Bover
In reply to this post by Засим Александр
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 Засим Александр <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Array.prototype.count

Ranando King
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 <[hidden email]> 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 Засим Александр <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Array.prototype.count

Andrea Giammarchi-2
[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 <[hidden email]> 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 <[hidden email]> 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 Засим Александр <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss