Kebab file names

This commit is contained in:
Angelos Chalaris
2023-04-27 21:58:35 +03:00
parent 1d189c709a
commit 61200d90c4
440 changed files with 0 additions and 0 deletions

24
snippets/map-object.md Normal file
View File

@ -0,0 +1,24 @@
---
title: Map array to object
tags: array,object
cover: two-lighthouses
firstSeen: 2017-12-18T12:11:58+02:00
lastUpdated: 2020-10-21T21:54:53+03:00
---
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 }
```