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
function groupBy(items, key) {
return items.reduce((accumulator, current) => {
const groupKey = current[key];
if (!accumulator[groupKey]) {
accumulator[groupKey] = [];
}
accumulator[groupKey].push(current);
return accumulator;
}, {});
}
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!