From 2f8b85cbe8949fadaa8419f9827a3ff899ea0949 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Wed, 30 May 2018 16:12:29 -0700 Subject: [PATCH 1/6] Add toHash snippet A thing I've found myself using a LOT recently to create single pass closures so that I can grab a referenced object based on an identifier in another array. Useful for unfolding loops so you only have to loop over a dataset once before referencing them later --- snippets/toHash | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/toHash diff --git a/snippets/toHash b/snippets/toHash new file mode 100644 index 000000000..4df2844be --- /dev/null +++ b/snippets/toHash @@ -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" } ] } ] +``` From 32beb277824a833b6597ecfe310be06c261b8563 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Wed, 30 May 2018 16:14:04 -0700 Subject: [PATCH 2/6] Rename toHash to toHash.md --- snippets/{toHash => toHash.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{toHash => toHash.md} (100%) diff --git a/snippets/toHash b/snippets/toHash.md similarity index 100% rename from snippets/toHash rename to snippets/toHash.md From de3f900b344f3399859e2821f7c004470e90a549 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Wed, 30 May 2018 16:15:13 -0700 Subject: [PATCH 3/6] Update toHash.md --- snippets/toHash.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/snippets/toHash.md b/snippets/toHash.md index 4df2844be..32b5437af 100644 --- a/snippets/toHash.md +++ b/snippets/toHash.md @@ -15,5 +15,13 @@ toHash([ { a: 'label' } ], 'a'); // { label: { a: 'label' } } 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" } ] } ] +managers.forEach( manager => + manager.employees = manager.employees.map( + function( id ){ + return this[id]; + }, + toHash( users, 'id' ) + ) +); +managers // [ { manager:1, employees: [ { id: 2, first: "Joe" }, { id: 3, first: "Moe" } ] } ] ``` From 01a3266facf4054e6bf6d6ea19b6887e92643128 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Thu, 31 May 2018 10:56:07 -0700 Subject: [PATCH 4/6] Use call on Reduce to apply it to array likes --- snippets/toHash.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/snippets/toHash.md b/snippets/toHash.md index 32b5437af..9343c1e51 100644 --- a/snippets/toHash.md +++ b/snippets/toHash.md @@ -1,10 +1,12 @@ ### toHash -Reduces a given iterable type into a value hash(Object by reference) by the given property or the current iteration +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 ```js const toHash = ( object, key ) => - object.reduce( ( acc, data, index ) => ( ( acc[ data[ key || index ] ] = data ), acc ), {} ) + Array.prototype.reduce.call( object, ( acc, data, index ) => ( ( acc[ !key ? index : data[ key ] ] = data ), acc ), {} ) ) ``` ```js @@ -14,7 +16,7 @@ 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 +// We use function here because we want a bindable reference but a closure referencing the hash would work too managers.forEach( manager => manager.employees = manager.employees.map( function( id ){ From e7611f9742405044a97ad7c05083ba230d436c3b Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Thu, 31 May 2018 10:57:28 -0700 Subject: [PATCH 5/6] Remove unneeded closing parenthesis --- snippets/toHash.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/toHash.md b/snippets/toHash.md index 9343c1e51..0711dc607 100644 --- a/snippets/toHash.md +++ b/snippets/toHash.md @@ -6,7 +6,7 @@ Given an Iterable or Array like structure we call Array.prototype.reduce.call on ```js const toHash = ( object, key ) => - Array.prototype.reduce.call( object, ( acc, data, index ) => ( ( acc[ !key ? index : data[ key ] ] = data ), acc ), {} ) ) + Array.prototype.reduce.call( object, ( acc, data, index ) => ( ( acc[ !key ? index : data[ key ] ] = data ), acc ), {} ) ``` ```js From 72a364576755c2f409821c8dee1b648edf436e6b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 31 May 2018 21:09:51 +0300 Subject: [PATCH 6/6] Update toHash.md --- snippets/toHash.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/toHash.md b/snippets/toHash.md index 0711dc607..1e176b15d 100644 --- a/snippets/toHash.md +++ b/snippets/toHash.md @@ -1,8 +1,8 @@ ### 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 const toHash = ( object, key ) => @@ -12,11 +12,10 @@ const toHash = ( object, key ) => ```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 +// 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 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 => manager.employees = manager.employees.map( function( id ){ @@ -25,5 +24,5 @@ managers.forEach( manager => 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" } ] } ] ```