Lodash _.countBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Creates an object whose keys come from running each element through an iteratee. Each key maps to a number — how many times that key was produced. Part of Lodash 4.x Collection (since 0.5.0).

Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.countBy on a collection. Start with this line.

Lodash
_.countBy(collection, [iteratee=_.identity])
CallWhat it does
_.countBy(collection, [iteratee=_.identity])Primary signature.
_.countBy(collection)Call with a collection argument.

Prefer import countBy from "lodash/countBy" when you want a smaller bundle.

Library declarations
Lodash
_.countBy(collection, [iteratee=_.identity])

Parameters

ParameterDescription
collectionRequired. Array or object to tally
iterateeOptional. Produces the key; default _.identity

Return Value

Return ValueDescription
*New object of numbers

Lodash Examples

Example 1 — Official: Count by Math.floor

Numbers sharing the same floor increment the same tally.

Lodash
_.countBy([6.1, 4.2, 6.3], Math.floor);
// → { "4": 1, "6": 2 }
Try It Yourself

How It Works

  1. Iteratee. Math.floor turns each number into an integer key.
  2. First 6.x. 6.1 opens tally "6" at 1.
  3. 4.2. Opens tally "4" at 1.
  4. Second 6.x. 6.3 bumps "6" to 2.

Example 2 — Official: Property Shorthand "length"

Count strings by character length.

Lodash
_.countBy(["one", "two", "three"], "length");
// → { "3": 2, "5": 1 }
Try It Yourself

How It Works

  1. Shorthand. "length" reads each string’s length property.
  2. Length 3. "one" and "two" make "3": 2.
  3. Length 5. "three" alone → "5": 1.
  4. No arrays. Unlike groupBy, you only get counts.

Example 3 — Count Objects by a Field

Tally rows by a shared property name.

Lodash
var users = [
  { id: 1, team: "alpha" },
  { id: 2, team: "beta" },
  { id: 3, team: "alpha" }
];

_.countBy(users, "team");
// → { alpha: 2, beta: 1 }
Try It Yourself

How It Works

  1. Key. Each object’s team string becomes the tally name.
  2. alpha. Users 1 and 3 → count 2.
  3. beta. User 2 alone → count 1.
  4. Lightweight. Ideal for dashboards that only need frequencies.

Example 4 — Custom Function Iteratee

Return any key you compute — here even vs odd.

Lodash
_.countBy([1, 2, 3, 4], function (n) {
  return n % 2 === 0 ? "even" : "odd";
});
// → { odd: 2, even: 2 }
Try It Yourself

How It Works

  1. Predicate key. Function returns "even" or "odd".
  2. Odds. 1 and 3 → odd: 2.
  3. Evens. 2 and 4 → even: 2.
  4. Flexible. Any string/number/boolean key works (coerced to string).

Example 5 — Count Values of a Plain Object

Lodash walks object values and tallies them the same way.

Lodash
_.countBy({ a: 1, b: "hi", c: 2, d: true }, function (value) {
  return typeof value;
});
// → { number: 2, string: 1, boolean: 1 }
Try It Yourself

How It Works

  1. Values only. Keys a…d are not part of the tallies.
  2. typeof. Each value’s type string becomes the count key.
  3. Numbers. 1 and 2 share "number": 2.
  4. Singletons. String and boolean each count 1.

Browser & Runtime Support

_.countBy is a Lodash 4.x Collection helper (since 0.5.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. There is no single native Array.countBy in all environments — Lodash remains the portable option.

✓ Stable · Lodash 4.x

_.countBy()

Available in [email protected] full builds and the lodash/countBy modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.countBy Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import countBy from "lodash/countBy" for smaller builds. Ideal for frequency charts, status summaries, and quick tallies.

Frequently Asked Questions

_.countBy(collection, iteratee) builds an object whose keys come from iteratee(value). Each key maps to how many elements produced that key.
No. It returns a new plain object of numbers. The source collection is left unchanged.
Object keys are strings. Numeric results like 6 become "6" in the returned object.
countBy stores counts (numbers). groupBy stores arrays of the actual elements. Use groupBy when you still need the rows.
keyBy keeps one element per key (last wins). countBy never stores elements — only tallies.
Use import countBy from "lodash/countBy" or require("lodash/countBy") for tree-shaking. Full build: import _ from "lodash" then _.countBy.

Next: _.every()

Test whether every element in a collection passes a predicate.

_.every() tutorial →

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.

10 people found this page helpful