AniUI Academy
easy+150 XPPractice

Count items by a computed key

Filter sidebars show counts: "Open (12)", "Closed (3)". Those numbers come from tallying a list by some derived property rather than storing them separately.

Implement countBy(items, keyFn). For each item, call keyFn(item) to get its bucket, then return a plain object mapping each bucket to the number of items in it. Because these are object properties, the returned keys are strings: a keyFn returning the number 1 produces the key "1", and one returning true produces "true". Buckets with no items never appear. An empty input array returns an empty object.

What it has to do

  • Return a plain object mapping each key produced by keyFn to its count.
  • Keys are the string form of whatever keyFn returns, so numbers and booleans become strings.
  • Only keys that actually occur appear in the result.
  • Return {} for an empty 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.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.