Group a List by a Computed Key

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write groupBy(items, keyOf): return an object mapping each key that keyOf returns to the items with that key, in their original order. Type it generically: the groups hold the items' own type, and the object's keys are whatever keyOf can return (a union of strings, say), each possibly missing.

Your code must pass

  • Groups items by their key, in order
  • Returns an empty object for no items
  • Has no type errors
  • Keys are what keyOf returns
  • Groups hold the items' type