JS Built-ins
ObjectObject.groupByintermediate

Object.groupBy() β€” group numbers by even/odd

πŸ“– 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() β€” custom key function

The key function can return any string. A classic example is grouping numbers as 'even' or 'odd'.

Challenge: Implement groupEvenOdd(nums) that groups numbers into even and odd.

groupEvenOdd([1, 2, 3, 4]) // β†’ { odd: [1, 3], even: [2, 4] }
πŸ’‘ Hints (1)
  • β€’ Return `"even"` or `"odd"` from the key function based on `n % 2`.