Underscore.js _.indexBy() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Given a list and an iteratee (or property name), returns an object that indexes each item by key. Same idea as _.groupBy, but for when you know your keys are unique (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 _.indexBy. Start with this line.

Underscore
_.indexBy(list, iteratee, [context])
CallWhat it does
_.indexBy(list, iteratee, [context])Primary signature.
_.indexBy(...)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
_.indexBy(list, iteratee, [context])

Parameters

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

Return Value

Return ValueDescription
*Object: key → single item

Underscore.js Examples

Example 1 — Official: Property Shorthand

Pass a string to index each object by that property.

Underscore
var stooges = [
  { name: "moe", age: 40 },
  { name: "larry", age: 50 },
  { name: "curly", age: 60 }
];

_.indexBy(stooges, "age");
// → {
//   "40": { name: "moe", age: 40 },
//   "50": { name: "larry", age: 50 },
//   "60": { name: "curly", age: 60 }
// }
Try It Yourself

How It Works

  1. Shorthand. "age" means use each object’s .age as the key.
  2. One item. Each age maps to that stooge object — not an array.
  3. Lookup. Later you can do byAge["50"] to get larry.
  4. Keys as strings. Object keys are stringified ("40", not number 40).

Example 2 — Lookup by id

Common pattern: turn a list into a map keyed by unique id.

Underscore
var people = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Linus" },
  { id: 3, name: "Grace" }
];

var byId = _.indexBy(people, "id");
byId["2"].name;
// → "Linus"
Try It Yourself

How It Works

  1. Index. Each person’s id becomes a key in the result.
  2. O(1) read. byId["2"] returns the Linus object directly.
  3. No scan. You avoid looping the array every time you need one person.
  4. Refs. Objects in the map are the same instances as in people.

Example 3 — Function Iteratee

Pass a function when the key is computed, not a plain property.

Underscore
var files = [
  { path: "a.txt", size: 10 },
  { path: "b.md", size: 20 }
];

_.indexBy(files, function (f) {
  return f.path.split(".").pop();
});
// → {
//   "txt": { path: "a.txt", size: 10 },
//   "md":  { path: "b.md", size: 20 }
// }
Try It Yourself

How It Works

  1. Iteratee. Splits each path and takes the extension as the key.
  2. txt / md. Each extension maps to that one file object.
  3. Uniqueness. Works here because each extension appears once.
  4. Duplicates? A second .txt would overwrite the first — use _.groupBy then.

Example 4 — _.indexBy vs _.groupBy

Same keys — one item (last wins) vs an array of every match.

Underscore
var pets = [
  { name: "Rex", type: "dog" },
  { name: "Mia", type: "cat" },
  { name: "Buddy", type: "dog" }
];

_.indexBy(pets, "type");
// → { dog: { name: "Buddy", type: "dog" },
//     cat: { name: "Mia", type: "cat" } }

_.groupBy(pets, "type");
// → { dog: [Rex, Buddy], cat: [Mia] }
Try It Yourself

How It Works

  1. Shared key. Two dogs share type: "dog".
  2. indexBy. Only Buddy remains under dog (last write wins).
  3. groupBy. Both Rex and Buddy stay in an array.
  4. Pick by intent. Unique keys → indexBy; shared keys → groupBy.

Browser & Runtime Support

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

_.indexBy()

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

_.indexBy(list, iteratee, [context]) builds a lookup object. Each key from the iteratee maps to a single item — ideal when keys are unique (like id or age with no duplicates).
Yes. A string iteratee uses that property as the key — e.g. _.indexBy(stooges, 'age').
Same key logic, but _.indexBy stores one item per key. _.groupBy stores an array of every item that shares a key.
The last item wins. Later values overwrite earlier ones for that key — so prefer unique keys, or use _.groupBy when duplicates are expected.
No. It returns a new object. Object elements inside are still shared by reference.
An empty object {}.

Next: _.countBy()

Tally how many items share each key — counts instead of items.

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