JS Built-ins
ObjectObject.groupBybeginner

Object.groupBy() β€” empty array

πŸ“– 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() β€” empty input

Calling Object.groupBy with an empty array returns an empty object {}.

Challenge: Implement groupEmpty() that calls Object.groupBy([], () => 'x') and returns the result.

groupEmpty() // β†’ {}
πŸ’‘ Hints (1)
  • β€’ An empty iterable always results in no groups.