Files
30-seconds-of-code/snippets/countOccurrences.md
Brian Douglas 39d99a4723 Update countOccurrences function
Removes unnecessary addition of `0`.
2018-06-05 13:25:49 +01: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, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
```
```js
countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3
```