AniUI Academy
Live noweasy+150 XPCloses Sunday

Build your own groupBy

Almost every list you render is grouped by something: orders by status, students by year, commits by author. Write the helper once and you stop hand-rolling it.

Implement groupBy(items, keyFn). It takes an array and a function that returns the grouping key for an item, and returns an object mapping each key to the array of items that produced it.

What it has to do

  • Preserve the original order of items inside each group.
  • Handle an empty array by returning an empty object.
  • Accept keys that are numbers as well as strings.
  • Do not mutate the input array.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

Stuck? The javascript course covers everything this challenge needs.