Lodash _.groupBy() method
What you’ll learn
- How
_.groupBy(collection, iteratee)builds arrays of elements keyed by iteratee output. - Using shorthand iteratees (
"prop", paths) versus custom functions. - When to prefer
_.countBy,_.keyBy, or_.partitioninstead. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
See _.countBy() for the same iteratee rules applied to tallies instead of element lists.
- You are comfortable with plain objects as maps from keys to values.
- You can open Try-it labs or run snippets locally.
Overview
_.groupBy is the workhorse for “split this list into piles”: dashboards by status, bucketing scores, grouping API rows before rendering sections—without hand-writing accumulator loops.
Buckets of rows
Each unique iteratee result maps to an array of every element that landed in that bucket.
Familiar iteratees
Property strings, path arrays, and callbacks behave like map or filter.
Objects too
Iterate plain-object collections with (value, key, collection) callbacks.
Syntax
_.groupBy(collection, iteratee) - collection: array, array-like, or plain object Lodash can iterate.
- iteratee: invoked per element as
(value, index|key, collection); its result selects the bucket key. - Returns: plain object mapping string keys to arrays of grouped elements.
Group strings by length
A function iteratee buckets each word alongside others that share the same character count.
import groupBy from "lodash/groupBy";
groupBy(["apple", "pie", "banana", "cat"], (word) => word.length);
// → { "5": ["apple"], "3": ["pie", "cat"], "6": ["banana"] } Shorthand: group records by team
A string iteratee plucks that property from each object—ideal for normalizing API payloads.
import groupBy from "lodash/groupBy";
groupBy(
[
{ id: 1, team: "alpha" },
{ id: 2, team: "beta" },
{ id: 3, team: "alpha" }
],
"team"
);
// → { alpha: [...], beta: [...] } Bucket values from a plain object
Collections are not limited to arrays—Lodash walks enumerable entries and groups by your iteratee.
import groupBy from "lodash/groupBy";
groupBy({ a: 1, b: "hi", c: 2, d: true }, (value) => typeof value);
// → { number: [1, 2], string: ["hi"], boolean: [true] } 📋 _.groupBy vs countBy, keyBy, partition
| API | Shape of result | Best when |
|---|---|---|
_.groupBy(collection, iteratee) | Keys → arrays of elements | You need every row per bucket |
_.countBy(collection, iteratee) | Keys → counts | Histograms without retaining elements |
_.keyBy(collection, iteratee) | Keys → single element | One canonical row per key (last wins) |
_.partition(collection, predicate) | [matches, rejects] | Exactly two piles by true/false |
Pitfalls to avoid
Collisions after coercion
Different iteratee values can stringify to the same object property—design buckets accordingly.
Reached for keyBy instead?
If each bucket should hold only one element, keyBy avoids nested arrays and accidental duplicates.
Huge groups in memory
Every element stays referenced in its array—stream or aggregate when datasets grow past comfort.
❓ FAQ
Summary
- Purpose:
_.groupBy(collection, iteratee)groups elements into arrays keyed by iteratee output. - Contrast: use
countByfor counts only; usekeyBywhen keys map to a single record. - Next: Lodash _.includes(), Lodash _.forEachRight() (previous), or collection hub.
_.groupBy returns an object whose values are arrays of elements sharing the same iteratee key (after string coercion). Pair it with _.countBy when you only need counts, or _.keyBy when each bucket should collapse to a single row.
6 people found this page helpful
