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])
Call
What 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
Parameter
Description
collection
Required. Array or object to tally
iteratee
Optional. Produces the key; default _.identity
Return Value
Return Value
Description
*
New object of numbers
Lodash Examples
Example 1 — Official: Count by Math.floor
Numbers sharing the same floor increment the same tally.
Values only. Keys a…d are not part of the tallies.
typeof. Each value’s type string becomes the count key.
Numbers.1 and 2 share "number": 2.
Singletons. String and boolean each count 1.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.countByLodash 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.