Lodash _.countBy() method

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

What you’ll learn

  • How _.countBy(collection, iteratee) builds tallies keyed by iteratee results.
  • Using shorthand iteratees ("prop", ["a","b"]) versus functions.
  • How this differs from _.groupBy (arrays per bucket) and hand-rolled reduce.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Skim the collection methods hub for how Lodash treats arrays and plain objects as collections.

  • You understand plain objects as key/value maps.
  • You can open Try-it labs or run snippets locally.

Overview

_.countBy answers “how many per bucket?” in one pass—ideal for histogram-style summaries, tag frequency, or bucketing numeric scores without retaining every element.

Compact tallies

Only counts per group land in the result—lighter than storing arrays when you just need totals.

Iteratee flexibility

Functions, property names, or path arrays behave like other Lodash collection helpers.

Arrays or objects

The same API walks arrays or enumerable object properties consistently.

Syntax

javascript
_.countBy(collection, iteratee)
  • collection: array, array-like, or plain object Lodash can iterate.
  • iteratee: invoked per element as (value, key|index, collection); result selects the tally bucket.
  • Returns: plain object mapping string keys to integer counts.
1

Bucket numbers with a function iteratee

Floor scores into decade-like bins so each distinct rounded key receives its own running total.

javascript
import countBy from "lodash/countBy";

countBy([83, 87, 91, 44, 49], (n) => Math.floor(n / 10) * 10);
// → { "80": 2, "90": 1, "40": 2 }
Try it Yourself
2

Shorthand: count rows by status

Passing a string iteratee plucks that property from each object—handy for API payloads.

javascript
import countBy from "lodash/countBy";

countBy(
  [
    { id: 1, status: "open" },
    { id: 2, status: "done" },
    { id: 3, status: "open" }
  ],
  "status"
);
// → { open: 2, done: 1 }
Try it Yourself
3

Walk a plain object collection

Collections are not limited to arrays—Lodash iterates values and passes both value and key to your iteratee.

javascript
import countBy from "lodash/countBy";

countBy(
  { alice: { role: "admin" }, bob: { role: "user" }, carol: { role: "admin" } },
  (rec) => rec.role
);
// → { admin: 2, user: 1 }
Try it Yourself

📋 _.countBy vs groupBy, reduce

APIShape of resultBest when
_.countBy(collection, iteratee)Keys → countsYou only need totals per bucket
_.groupBy(collection, iteratee)Keys → element arraysYou still need the grouped rows
_.reduce(collection, fn, {})Custom accumulatorLogic branches beyond simple tally keys

Pitfalls to avoid

Keys

String coercion collisions

Distinct iteratee values can stringify to the same object key—know how your buckets serialize.

Data

Sparse arrays

Holes may be skipped depending on engine paths; normalize arrays before counting critical metrics.

Need

Need items back?

If downstream steps require filtered subsets per bucket, start from groupBy instead of recomputing.

❓ FAQ

No. Lodash builds a new plain object of tallies; your input array or object stays unchanged.
Values are numbers—the number of elements whose iteratee resolved to the same key (after coercion to string keys).
Lodash uses the iteratee result as the grouping signal; object keys in the result are stringified (including for compound keys).
countBy stores counts per group; groupBy stores arrays of elements per group. Pick groupBy when you still need the underlying rows.
Yes—Lodash walks own enumerable string keys and invokes your iteratee as (value, key, collection).

Summary

Did you know?

_.countBy turns each iteratee result into an object key (via String coercion). Objects or arrays as keys become stable string labels—pair that mental model with _.groupBy when you need the raw items per bucket.

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