Lodash _.groupBy() 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 an array of elements that produced that key. Order inside each array follows collection order. Part of Lodash 4.x Collection (since 0.1.0).

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

Syntax

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

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

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

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

Parameters

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

Return Value

Return ValueDescription
*New object of arrays

Lodash Examples

Example 1 — Official: Group by Math.floor

Numbers sharing the same floor land in the same array.

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

How It Works

  1. Iteratee. Math.floor turns each number into an integer key.
  2. First 6.x. 6.1 opens bucket "6".
  3. 4.2. Opens bucket "4".
  4. Second 6.x. 6.3 appends to the existing "6" array.

Example 2 — Official: Property Shorthand "length"

Group strings by character length.

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

How It Works

  1. Shorthand. "length" reads each string’s length property.
  2. Length 3. "one" and "two" share bucket "3".
  3. Length 5. "three" alone in "5".
  4. Order. Within "3", one appears before two.

Example 3 — Group Objects by a Field

Bucket rows by a shared property name.

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

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

How It Works

  1. Key. Each object’s team string becomes the bucket name.
  2. alpha. Users 1 and 3 share that key.
  3. beta. User 2 alone.
  4. Objects shared. Bucket arrays hold the same object references.

Example 4 — Custom Function Iteratee

Return any key you compute — here even vs odd.

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

How It Works

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

Example 5 — Group Values of a Plain Object

Lodash walks object values and buckets them the same way.

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

How It Works

  1. Values only. Keys a…d are not part of the buckets.
  2. typeof. Each value’s type string becomes the group key.
  3. Numbers. 1 and 2 share "number".
  4. Singletons. String and boolean each get their own array.

Browser & Runtime Support

_.groupBy is a Lodash 4.x Collection helper (since 0.1.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.groupBy in all environments — Lodash remains the portable option.

✓ Stable · Lodash 4.x

_.groupBy()

Available in [email protected] full builds and the lodash/groupBy 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
_.groupBy Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import groupBy from "lodash/groupBy" for smaller builds. Ideal for UI filters, reporting buckets, and indexing by field.

Frequently Asked Questions

_.groupBy(collection, iteratee) builds an object whose keys come from iteratee(value). Each key maps to an array of elements that produced that key.
No. It returns a new plain object of arrays. The source collection is left unchanged.
Object keys are strings. Numeric results like 6 become "6" in the returned object.
groupBy keeps every element in arrays. countBy only stores how many items fell into each key.
keyBy keeps one element per key (last wins). groupBy accumulates all matching elements into arrays.
Lodash preserves the order they occur in the collection when pushing into each array.

Next: _.includes()

Test whether a collection (or string) contains a given value.

_.includes() 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