Update toHash.md
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
### toHash
|
### toHash
|
||||||
|
|
||||||
Reduces a given Array Like into a value hash(keyed data store)
|
Reduces a given Array-like into a value hash (keyed data store).
|
||||||
|
|
||||||
Given an Iterable or Array like structure we call Array.prototype.reduce.call on the provided object to step over it and return an Object keyed by the reference value
|
Given an Iterable or Array-like structure, call `Array.prototype.reduce.call()` on the provided object to step over it and return an Object, keyed by the reference value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toHash = ( object, key ) =>
|
const toHash = ( object, key ) =>
|
||||||
@ -12,11 +12,10 @@ const toHash = ( object, key ) =>
|
|||||||
```js
|
```js
|
||||||
toHash([ 4,3,2,1 ]); // { 0: 4, 1: 3, 2: 2, 1: 1 }
|
toHash([ 4,3,2,1 ]); // { 0: 4, 1: 3, 2: 2, 1: 1 }
|
||||||
toHash([ { a: 'label' } ], 'a'); // { label: { a: 'label' } }
|
toHash([ { a: 'label' } ], 'a'); // { label: { a: 'label' } }
|
||||||
|
// A more in depth example:
|
||||||
// A more in depth example
|
|
||||||
let users = [ { id: 1, first: 'Jon' }, { id: 2, first: 'Joe' }, { id: 3, first: 'Moe' } ];
|
let users = [ { id: 1, first: 'Jon' }, { id: 2, first: 'Joe' }, { id: 3, first: 'Moe' } ];
|
||||||
let managers = [ { manager: 1, employees: [ 2, 3 ] } ];
|
let managers = [ { manager: 1, employees: [ 2, 3 ] } ];
|
||||||
// We use function here because we want a bindable reference but a closure referencing the hash would work too
|
// We use function here because we want a bindable reference, but a closure referencing the hash would work, too.
|
||||||
managers.forEach( manager =>
|
managers.forEach( manager =>
|
||||||
manager.employees = manager.employees.map(
|
manager.employees = manager.employees.map(
|
||||||
function( id ){
|
function( id ){
|
||||||
@ -25,5 +24,5 @@ managers.forEach( manager =>
|
|||||||
toHash( users, 'id' )
|
toHash( users, 'id' )
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
managers // [ { manager:1, employees: [ { id: 2, first: "Joe" }, { id: 3, first: "Moe" } ] } ]
|
managers; // [ { manager:1, employees: [ { id: 2, first: "Joe" }, { id: 3, first: "Moe" } ] } ]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user