22 lines
554 B
Markdown
22 lines
554 B
Markdown
---
|
|
title: frequencies
|
|
tags: array,object,intermediate
|
|
---
|
|
|
|
Creates an object with the unique values of an array as keys and their frequencies as the values.
|
|
|
|
- Use `Array.prototype.reduce()` to map unique values to an object's keys, adding to existing keys every time the same value is encountered.
|
|
|
|
```js
|
|
const frequencies = arr =>
|
|
arr.reduce((a, v) => {
|
|
a[v] = a[v] ? a[v] + 1 : 1;
|
|
return a;
|
|
}, {});
|
|
```
|
|
|
|
```js
|
|
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']); // { a: 4, b: 2, c: 1 }
|
|
frequencies([...'ball']); // { b: 1, a: 1, l: 2 }
|
|
```
|