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

28
snippets/index-by.md Normal file
View File

@ -0,0 +1,28 @@
---
title: Index array based on function
tags: array,object
author: chalarangelo
cover: guitar-living-room
firstSeen: 2021-06-20T05:00:00-04:00
---
Creates an object from an array, using a function to map each value to a key.
- Use `Array.prototype.reduce()` to create an object from `arr`.
- Apply `fn` to each value of `arr` to produce a key and add the key-value pair to the object.
```js
const indexBy = (arr, fn) =>
arr.reduce((obj, v, i) => {
obj[fn(v, i, arr)] = v;
return obj;
}, {});
```
```js
indexBy([
{ id: 10, name: 'apple' },
{ id: 20, name: 'orange' }
], x => x.id);
// { '10': { id: 10, name: 'apple' }, '20': { id: 20, name: 'orange' } }
```