Scriptdrill

Level 2: Arrays and objects Exercise 10 of 45 Pro

Group by a field

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return an object that maps each value of `item[key]` to the array of items with that value, in their original order. Values become object keys, so the number 2 becomes the key `"2"`. Keys are case-sensitive: `"Paid"` and `"paid"` are different groups.

What this exercise teaches

One pass over the items is enough: look up the group for item[key], create it when missing, and push. Object keys are strings, so numbers and booleans turn into "2" or "true" automatically.

A hint if you get stuck

Start with `{}` and loop over the items. Create the group array the first time you see a value, then push the item into it.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Arrays and objects