Full answer
Grouping means turning a flat list into buckets keyed by some value, for example students by grade or orders by city.
The modern tool is Object.groupBy(items, callback), available in Node.js 21 and recent browsers. The callback receives each item and returns the key. The result is an object with a null prototype whose values are arrays. Map.groupBy does the same but returns a Map, which supports keys of any type and keeps insertion order.
The older, portable way is reduce. The accumulator starts as an empty object or Map. For every item, the callback finds the key, creates the array if needed, and pushes the item. This pattern works everywhere and shows the idea behind the new methods.
Points worth saying aloud: the groups contain references to the same item objects, not copies. Keys of a plain object become strings, so grouping by a number gives string keys. A null-prototype result avoids clashes with names like constructor. Counting is a small variation: store a number instead of an array.
The work is one pass over the data, so the time is O(n) and the extra space is O(n).
const students = [
{ name: 'Asha', grade: 'B', marks: 72 },
{ name: 'Ravi', grade: 'A', marks: 91 },
{ name: 'Meera', grade: 'B', marks: 68 },
{ name: 'Dev', grade: 'A', marks: 88 },
{ name: 'Isha', grade: 'C', marks: 45 },
];
const byGrade = Object.groupBy(students, (student) => student.grade);
console.log(Object.keys(byGrade), byGrade.A.map((s) => s.name));
const byBand = Map.groupBy(students, (s) => (s.marks >= 80 ? 80 : 40));
console.log([...byBand.keys()], byBand.get(80).length);
function groupWithReduce(list, getKey) {
return list.reduce((groups, item) => {
const key = getKey(item);
(groups[key] ??= []).push(item);
return groups;
}, {});
}
const manual = groupWithReduce(students, (s) => s.grade);
console.log(Object.keys(manual).map((key) => `${key}:${manual[key].length}`));
const counts = students.reduce((acc, s) => acc.set(s.grade, (acc.get(s.grade) ?? 0) + 1), new Map());
console.log(counts);
console.log(byGrade.A[0] === students[1], Object.getPrototypeOf(byGrade));Run with Node.js v24.22.1 as js-36.js; two runs gave identical output. Output: [ 'B', 'A', 'C' ] [ 'Ravi', 'Dev' ] [ 40, 80 ] 2 [ 'B:2', 'A:2', 'C:1' ] Map(3) { 'B' => 2, 'A' => 2, 'C' => 1 } true null
Key points
- Object.groupBy and Map.groupBy group in one call.
- reduce with an accumulator is the portable alternative.
- Object keys become strings; Map keeps the original key type.
- Groups hold references to the original objects.
- One pass: O(n) time and O(n) extra space.
Common mistakes
- Forgetting to create the array for a new key before pushing.
- Using a plain object when the keys are objects or need to stay numbers.
- Mutating items inside a group and not realizing the original list changes too.
- Using Object.groupBy in an environment that does not have it yet without a fallback.
Follow-ups
- How would you group by two keys at once?
- What does the result of Object.groupBy inherit from, and why does that matter?
- How would you group and then compute the average of each group?
Tips
- Show the reduce version first if the interviewer's runtime is unknown, then mention groupBy.
- Say the time and space complexity as O(n) and O(n).
- Use ??= for the "create if missing" step; it is short and shows modern syntax.