Array group by
Group items.
- Snippet
- JavaScript and TypeScript
- 2 blanks to fill
const grouped = items.reduce((acc, item) => {
(acc[item.key] ||= []).push(item);
return acc;
}, {}); Orange words are blanks you have not filled yet. Press ⌘ Return to copy.
About this template
Group items.
- Category
- Code snippets
- Pack
- JavaScript and TypeScript
- Type
- Snippet, a short message
- Asks for
- items, key
Use it anywhere you write: email, chat, a document or a support tool. If you send it often, save the finished text as a snippet in ClipBuddy and paste it again from your clipboard history with ⌘⇧V.
Template source
The template as written, with its blanks in double braces, such as {{Name}}.
const grouped = {{items}}.reduce((acc, item) => {
(acc[item.{{key}}] ||= []).push(item);
return acc;
}, {});