Files
30-seconds-of-code/snippets/mapObject.md
Isabelle Viktoria Maciohsek ce48fffb4d Update snippet descriptions
2020-10-21 21:54:53 +03:00

22 lines
505 B
Markdown

---
title: mapObject
tags: array,object,intermediate
---
Maps the values of an array to an object using a function.
- Use `Array.prototype.reduce()` to apply `fn` to each element in `arr` and combine the results into an object.
- Use `el` as the key for each property and the result of `fn` as the value.
```js
const mapObject = (arr, fn) =>
arr.reduce((acc, el, i) => {
acc[el] = fn(el, i, arr);
return acc;
}, {});
```
```js
mapObject([1, 2, 3], a => a * a); // { 1: 1, 2: 4, 3: 9 }
```