Files
30-seconds-of-code/snippets/js/s/iterable-to-hash.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

27 lines
688 B
Markdown

---
title: Iterable to hash
type: snippet
language: javascript
tags: [array]
cover: pink-flower-tree
dateModified: 2022-01-30T12:45:30+03:00
---
Reduces a given iterable into a value hash (keyed data store).
- Use `Object.values()` to get the values of the iterable (object or array).
- Use `Array.prototype.reduce()` to iterate over the values and create an object, keyed by the reference value.
```js
const toHash = (object, key) =>
Object.values(object).reduce((acc, data, index) => {
acc[!key ? index : data[key]] = data;
return acc;
}, {});
```
```js
toHash([4, 3, 2, 1]); // { 0: 4, 1: 3, 2: 2, 3: 1 }
toHash([{ a: 'label' }], 'a'); // { label: { a: 'label' } }
```