JS Built-ins
ObjectObject.groupByintermediate

Object.groupBy() β€” group by first letter

πŸ“– Usage Example
// Group array items by a computed key
const items = [{ type: 'a', v: 1 }, { type: 'b', v: 2 }, { type: 'a', v: 3 }]
Object.groupBy(items, item => item.type)
// β†’ { a: [{...}, {...}], b: [{...}] }

Use Object.groupBy() to group an array's elements into an object keyed by a value returned from a callback.

Object.groupBy() β€” string key derived from item

You can derive the group key from any property of the item. Grouping words by their first letter is a natural example.

Challenge: Implement groupByFirstLetter(words) that groups words by their first character.

groupByFirstLetter(['apple', 'ant', 'banana'])
// β†’ { a: ['apple', 'ant'], b: ['banana'] }
πŸ’‘ Hints (1)
  • β€’ Return `w[0]` (or `w.charAt(0)`) from the key function.