Rename js snippets

This commit is contained in:
Angelos Chalaris
2023-05-19 20:23:47 +03:00
parent 82a614e42e
commit 9d032ce05e
305 changed files with 70 additions and 70 deletions

View File

@ -0,0 +1,26 @@
---
title: Value frequencies
type: snippet
language: javascript
tags: [array,object]
author: chalarangelo
cover: tropical-waterfall
dateModified: 2020-10-19T22:49:51+03:00
---
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 }
```