Last active
December 7, 2024 21:08
-
-
Save nblackburn/138fc48908c20fef64b436b4c04d5c1d to your computer and use it in GitHub Desktop.
Revisions
-
nblackburn revised this gist
Jul 24, 2024 . 2 changed files with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,4 +10,5 @@ const data = [ ]; groupBy(data, (d) => d.id); // { a: [ { id: 'a' }, { id: 'a' } ], b: [ { id: 'b' } ] } groupByArray(data, (d) => d.id); // [ { key: 'a', items: [{ id: 'a' }, { id: 'a' }] }, { key: 'b', items: [{ id: 'b' }] } ] ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,4 +16,15 @@ const groupBy = (items, callback) => { }); return groups; }; const groupByArray = (items, callback) => { const groups = groupBy(items, callback); return Object.keys(groups).map((group) => { return { key: group, items: groups[group] }; }); }; -
nblackburn revised this gist
May 14, 2024 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,13 +3,16 @@ const groupBy = (items, callback) => { items.forEach((item) => { const key = callback(item) ?? 'ungrouped'; const keys = Array.isArray(key) ? key : [key]; // Create the group if it doesn't exist. keys.forEach((key) => { if (!groups[key]) { groups[key] = []; } groups[key].push(item); }); }); return groups; -
nblackburn revised this gist
May 14, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ const groupBy = (items, callback) => { const groups = {}; items.forEach((item) => { const key = callback(item) ?? 'ungrouped'; // Create the group if it doesn't exist. if (!groups[key]) { -
nblackburn created this gist
May 14, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ ## Group By Group items by the key returned in the callback. ```javascript const data = [ { id: 'a' }, { id: 'b' }, { id: 'a' } ]; groupBy(data, (d) => d.id); // { a: [ { id: 'a' }, { id: 'a' } ], b: [ { id: 'b' } ] } ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ const groupBy = (items, callback) => { const groups = {}; items.forEach((item) => { const key = callback(item); // Create the group if it doesn't exist. if (!groups[key]) { groups[key] = []; } groups[key].push(item); }); return groups; };