Rename toHash to toHash.md

This commit is contained in:
Robert Mennell
2018-05-30 16:14:04 -07:00
committed by GitHub
parent 25038e7f03
commit 43efafa691

19
snippets/toHash.md Normal file
View File

@ -0,0 +1,19 @@
### toHash
Reduces a given iterable type into a value hash(Object by reference) by the given property or the current iteration
```js
const toHash = ( object, key ) =>
object.reduce( ( acc, data, index ) => ( ( acc[ data[ key || index ] ] = data ), acc ), {} )
```
```js
toHash([ 4,3,2,1 ]); // { 0: 4, 1: 3, 2: 2, 1: 1 }
toHash([ { a: 'label' } ], 'a'); // { label: { a: 'label' } }
// A more in depth example
let users = [ { id: 1, first: 'Jon' }, { id: 2, first: 'Joe' }, { id: 3, first: 'Moe' } ];
let managers = [ { manager: 1, employees: [ 2, 3 ] } ];
// We use function here because we need a bindable reference
managers.forEach( manager => manager.employees = manager.employees.map( function( id ){ return this[id]; }, toHash( users, 'id' ) ) ); // [ { manager:1, employees: [ { id: 2, first: "Joe" }, { id: 3, first: "Moe" } ] } ]
```