Lodash _.groupBy() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 _.partition instead.
  • 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

javascript
_.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.
1

Group strings by length

A function iteratee buckets each word alongside others that share the same character count.

javascript
import groupBy from "lodash/groupBy";

groupBy(["apple", "pie", "banana", "cat"], (word) => word.length);
// → { "5": ["apple"], "3": ["pie", "cat"], "6": ["banana"] }
Try it Yourself
2

Shorthand: group records by team

A string iteratee plucks that property from each object—ideal for normalizing API payloads.

javascript
import groupBy from "lodash/groupBy";

groupBy(
  [
    { id: 1, team: "alpha" },
    { id: 2, team: "beta" },
    { id: 3, team: "alpha" }
  ],
  "team"
);
// → { alpha: [...], beta: [...] }
Try it Yourself
3

Bucket values from a plain object

Collections are not limited to arrays—Lodash walks enumerable entries and groups by your iteratee.

javascript
import groupBy from "lodash/groupBy";

groupBy({ a: 1, b: "hi", c: 2, d: true }, (value) => typeof value);
// → { number: [1, 2], string: ["hi"], boolean: [true] }
Try it Yourself

📋 _.groupBy vs countBy, keyBy, partition

APIShape of resultBest when
_.groupBy(collection, iteratee)Keys → arrays of elementsYou need every row per bucket
_.countBy(collection, iteratee)Keys → countsHistograms without retaining elements
_.keyBy(collection, iteratee)Keys → single elementOne canonical row per key (last wins)
_.partition(collection, predicate)[matches, rejects]Exactly two piles by true/false

Pitfalls to avoid

Keys

Collisions after coercion

Different iteratee values can stringify to the same object property—design buckets accordingly.

Single row

Reached for keyBy instead?

If each bucket should hold only one element, keyBy avoids nested arrays and accidental duplicates.

Perf

Huge groups in memory

Every element stays referenced in its array—stream or aggregate when datasets grow past comfort.

❓ FAQ

No. Lodash builds a new plain object of arrays; your input array or object stays unchanged.
String-coerced iteratee results. Numeric buckets become string keys like "42" in the returned object.
groupBy keeps every element in arrays per bucket; countBy only stores how many fell into each bucket.
keyBy expects one element per key—the last collision wins. groupBy accumulates every element into arrays.
Lodash generally preserves visitation order when pushing into buckets—rely on docs/fixtures if exact ordering is part of your contract.

Summary

Did you know?

_.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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful