Files
30-seconds-of-code/snippets/countOccurrences.md
Angelos Chalaris 97c250bcfb Updated examples
2017-12-27 16:35:25 +02:00

14 lines
332 B
Markdown

### countOccurrences
Counts the occurrences of a value in an array.
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
```js
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
```
```js
countOccurrences([1,1,2,1,2,3], 1) // 3
```