Files
30-seconds-of-code/snippets/countOccurrences.md
2017-12-28 08:30:19 +00:00

14 lines
340 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
```