Underscore.js _.groupBy() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string, groups by that property (official Underscore docs).

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

Syntax

Call _.groupBy. Start with this line.

Underscore
_.groupBy(list, iteratee, [context])
CallWhat it does
_.groupBy(list, iteratee, [context])Primary signature.
_.groupBy(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.groupBy(list, iteratee, [context])

Parameters

ParameterDescription
listRequired. Collection to split
iterateeRequired. Function or property name that builds the group key
contextOptional. this for a function iteratee

Return Value

Return ValueDescription
*Object: key → array of items

Underscore.js Examples

Example 1 — Official: Group by Function

Pass an iteratee that returns the group key for each value.

Underscore
_.groupBy([1.3, 2.1, 2.4], function (num) {
  return Math.floor(num);
});
// → { 1: [1.3], 2: [2.1, 2.4] }
Try It Yourself

How It Works

  1. Iteratee. Math.floor turns each number into an integer key.
  2. First key. 1.3 → 1 → starts array [1.3].
  3. Shared key. 2.1 and 2.4 both map to 2.
  4. Shape. Result is an object of arrays, not a nested array list.

Example 2 — Official: Property Shorthand

Pass a string to group by that property on each value.

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

How It Works

  1. Shorthand. "length" means use each string’s .length.
  2. Key 3. "one" and "two" share length 3.
  3. Key 5. "three" alone in its group.
  4. Keys as strings. Object keys are stringified ("3", "5").

Example 3 — Group Objects by Field

Common pattern: bucket records by a category property.

Underscore
var items = [
  { name: "Apple", type: "fruit" },
  { name: "Carrot", type: "veg" },
  { name: "Mango", type: "fruit" }
];

_.groupBy(items, "type");
// → {
//   fruit: [{ name: "Apple", type: "fruit" },
//           { name: "Mango", type: "fruit" }],
//   veg:   [{ name: "Carrot", type: "veg" }]
// }
Try It Yourself

How It Works

  1. Property. Group key is each object’s type.
  2. fruit. Apple and Mango land in the same array.
  3. veg. Carrot alone in its group.
  4. Refs. Objects in the groups are the same instances as in items.

Example 4 — _.groupBy vs _.countBy

Same keys — items in arrays vs how many per key.

Underscore
var nums = [1, 2, 3, 4, 5];
var parity = function (num) {
  return num % 2 === 0 ? "even" : "odd";
};

_.groupBy(nums, parity);
// → { odd: [1, 3, 5], even: [2, 4] }

_.countBy(nums, parity);
// → { odd: 3, even: 2 }
Try It Yourself

How It Works

  1. Shared rule. Both use even/odd as the key.
  2. groupBy. Stores the actual numbers in each bucket.
  3. countBy. Stores only the size of each bucket.
  4. Pick by intent. Need the items → groupBy; need tallies → countBy.

Browser & Runtime Support

_.groupBy ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.groupBy()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.groupBy(list, iteratee, [context]) splits a collection into groups. It returns an object whose keys are the criteria from the iteratee and whose values are arrays of matching items.
Yes. If iteratee is a string, Underscore groups by that property on each value — e.g. _.groupBy(words, 'length').
_.groupBy allows shared keys, so each value is an array. _.indexBy assumes unique keys, so each value is a single item (last one wins).
Same grouping idea, but _.countBy stores a count (number) per key instead of the items themselves.
No. It returns a new object of arrays. Object elements inside are still shared by reference.
An empty object {}.

Next: _.indexBy()

Index a list by a unique key — one object per key, not an array.

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