Lodash _.keyBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Builds an object whose keys come from running each element through an iteratee. The value for each key is the last element that produced that key. Use it to turn an array of rows into a fast lookup map. Part of Lodash 4.x Collection (since 4.0.0).

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

Syntax

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

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

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

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

Parameters

ParameterDescription
collectionRequired. Array or object to iterate
iterateeOptional. Key maker; default _.identity

Return Value

Return ValueDescription
*New plain object (key → element)

Lodash Examples

Example 1 — Official: Property Shorthand

Index rows by the dir field.

Lodash
var array = [
  { dir: "left",  code: 97 },
  { dir: "right", code: 100 }
];

_.keyBy(array, "dir");
// → {
//     left:  { dir: "left",  code: 97 },
//     right: { dir: "right", code: 100 }
//   }
Try It Yourself

How It Works

  1. Shorthand. "dir" reads each object’s dir property.
  2. Keys. "left" and "right" become object keys.
  3. Values. Each value is the whole original object.
  4. Lookup. Use result.left instead of scanning the array.

Example 2 — Official: Function Iteratee

Derive keys from code with String.fromCharCode.

Lodash
var array = [
  { dir: "left",  code: 97 },
  { dir: "right", code: 100 }
];

_.keyBy(array, function (o) {
  return String.fromCharCode(o.code);
});
// → {
//     a: { dir: "left",  code: 97 },
//     d: { dir: "right", code: 100 }
//   }
Try It Yourself

How It Works

  1. Call. Iteratee receives each object as o.
  2. Transform. 97 → "a", 100 → "d".
  3. Store. Those characters become the result keys.
  4. Tip. Use a function when the key is computed, not a direct property.

Example 3 — Index Records by id

Common pattern: array from an API → object for O(1) lookup.

Lodash
_.keyBy(
  [
    { id: "a", score: 10 },
    { id: "b", score: 20 }
  ],
  "id"
);
// → { a: { id: "a", score: 10 }, b: { id: "b", score: 20 } }
Try It Yourself

How It Works

  1. Property. "id" is the unique key field.
  2. Map. Each id points at its full record.
  3. Access. result.a.score is 10 without a loop.
  4. No mutate. The source array is unchanged.

Example 4 — Duplicate Keys (Last Wins)

When two rows share a key, the later one replaces the earlier.

Lodash
_.keyBy(
  [
    { id: "a", score: 10 },
    { id: "a", score: 99 }
  ],
  "id"
);
// → { a: { id: "a", score: 99 } }
Try It Yourself

How It Works

  1. First. result.a becomes the score-10 row.
  2. Second. Same key "a" overwrites with score 99.
  3. Lost. The first row is no longer in the result.
  4. Fix. Need both? Use _.groupBy.

Example 5 — Key by String Length

Function iteratee with a collision on purpose.

Lodash
_.keyBy(["apple", "pie", "banana"], function (word) {
  return word.length;
});
// → { "3": "pie", "5": "apple", "6": "banana" }
// note: "apple" and any other length-5 word would collide
Try It Yourself

How It Works

  1. Lengths. 5, 3, 6 become keys (string-coerced).
  2. Values. Each key holds the matching word.
  3. Order. Object key order follows insertion of unique keys.
  4. vs groupBy. groupBy would keep arrays if lengths repeated.

Browser & Runtime Support

_.keyBy is a Lodash 4.x Collection helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native alternatives are hand-rolled reduce / Object.fromEntries patterns.

✓ Stable · Lodash 4.0+

_.keyBy()

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

Bottom line: Safe for production once Lodash is loaded. Prefer import keyBy from "lodash/keyBy" for smaller builds. Use when you need one element per key for fast lookups.

Frequently Asked Questions

_.keyBy(collection, iteratee) builds a plain object. Each key comes from the iteratee; the value is the element that produced that key.
No. It returns a new object. Elements themselves are not cloned.
The last element wins — earlier rows with the same key are overwritten.
groupBy stores arrays of all matches per key. keyBy stores one element per key.
Yes. Pass a string like "id" or "dir" as the iteratee shorthand.
Object keys are string-coerced. Prefer string or number keys you expect to look up later.

Next: _.map()

Transform each element and collect the results in an array.

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

9 people found this page helpful