Group By
JS Functions
Medium
15 min
arrays
objects
lodash
higher-order functions
Implement groupBy(array, iteratee). It runs every element of array through iteratee to get a key and returns an object that maps each key to an array of the elements that produced it, in their original order.
groupBy([6.1, 4.2, 6.3], Math.floor);
// { '4': [4.2], '6': [6.1, 6.3] }
groupBy(['one', 'two', 'three'], 'length');
// { '3': ['one', 'two'], '5': ['three'] }
const tasks = [
{ id: 1, owner: 'ana', done: false },
{ id: 2, owner: 'ben', done: true },
{ id: 3, owner: 'ana', done: true },
];
groupBy(tasks, (t) => t.owner);
// { ana: [{ id: 1, … }, { id: 3, … }], ben: [{ id: 2, … }] }
Requirements
iterateeis either:- a function, called with the element only (
iteratee(element)), or - a string, in which case the key is
element[iteratee].
- a function, called with the element only (
- Keys are converted to strings (
4becomes'4',undefinedbecomes'undefined'). - Elements in each group keep their original relative order, and are the same references as in the input (no cloning).
- Only keys that occur appear in the result. An empty array returns
{}. - Keys such as
'constructor','toString'or'hasOwnProperty'must produce normal groups. They must not collide with properties inherited fromObject.prototype. - Don't use
Object.groupByorMap.groupBy.
Follow-up
- ES2024 added
Object.groupByandMap.groupBy. What doesObject.groupByreturn that's different from a normal{}, and why? - How would you group by more than one level (for example by owner, then by status)?
- In a React list with thousands of rows, where would you run
groupByso it doesn't run on every render?
Spotted a mistake or a rights concern? Report an issue