javascriptNode.jsbeginner

Group Array by Key

Reducer pattern to group a collection of objects by a property

By ApeType Staff·10 lines·239 characters·1,420 total attempts
SOURCE CODE·javascript
function groupBy(items, key) { return items.reduce((accumulator, current) => { const groupKey = current[key]; if (!accumulator[groupKey]) { accumulator[groupKey] = []; } accumulator[groupKey].push(current); return accumulator; }, {}); }
UTF-8 · Tab indentationStandard Strict Parsing
TRANSCRIBE

Ready when you are.

Test your muscle memory and transcription speed against this exact snippet.

Start Typing
Tip: Press Esc anytime to restart

Best runs on this snippet

No recorded runs yet. Be the first to type this snippet!