Lodash _.mapKeys() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Creates an object with the same values as object and keys generated by running each own enumerable string key through iteratee. Part of Lodash Object since 3.8.0 (stable — not deprecated).

Warning: If two iteratee results collide, the later key silently overwrites the earlier one. Keep new keys unique.

Syntax

Call _.mapKeys. Start with this line.

Lodash
_.mapKeys(object, [iteratee])
CallWhat it does
_.mapKeys(object, [iteratee])Primary signature.
_.mapKeys(object)Work with an object’s keys, values, or paths.

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

Library declarations
Lodash
_.mapKeys(object, [iteratee])

Parameters

ParameterDescription
objectRequired. Source object to map
iterateeOptional. Produces each new key; default _.identity

Return Value

Return ValueDescription
*New object (source not mutated)

Lodash Examples

Example 1 — Official: key + value

Combine each key with its value to build the new key name.

Lodash
_.mapKeys({ a: 1, b: 2 }, function(value, key) {
  return key + value;
});
// → { 'a1': 1, 'b2': 2 }
Try It Yourself

How It Works

  1. Entry a. Iteratee gets (1, 'a') → returns 'a1'.
  2. Entry b. Iteratee gets (2, 'b') → returns 'b2'.
  3. Values. Still 1 and 2.
  4. New object. Source is not mutated.

Example 2 — Uppercase Keys

Normalize key casing while keeping values intact.

Lodash
_.mapKeys({ a: 1, b: 2 }, function(value, key) {
  return key.toUpperCase();
});
// → { A: 1, B: 2 }
Try It Yourself

How It Works

  1. Key only. Iteratee ignores value for the new name.
  2. toUpperCase. 'a' → 'A', 'b' → 'B'.
  3. Values. Unchanged at 1 and 2.
  4. Use. Handy for API key normalization.

Example 3 — Prefix Keys

Namespace keys before merging or exporting.

Lodash
_.mapKeys({ id: 10, name: 'x' }, function(value, key) {
  return 'user_' + key;
});
// → { user_id: 10, user_name: 'x' }
Try It Yourself

How It Works

  1. Prefix. Prepend 'user_' to each key.
  2. id. Becomes user_id with value 10.
  3. name. Becomes user_name with value 'x'.
  4. Unique. Prefixed names reduce collision risk.

Example 4 — Key Collision (Last Wins)

When two entries map to the same key, the later one overwrites.

Lodash
_.mapKeys({ a: 1, b: 2 }, function() {
  return 'x';
});
// → { x: 2 }
Try It Yourself

How It Works

  1. Same key. Both entries return 'x'.
  2. First write. x ← 1.
  3. Second write. x ← 2 overwrites.
  4. Silent. No error — design iteratees carefully.

Example 5 — mapKeys vs mapValues

Same source; rewrite keys or rewrite values.

Lodash
var obj = { a: 1, b: 2 };

_.mapKeys(obj, function(v, k) { return k.toUpperCase(); });
// → { A: 1, B: 2 }

_.mapValues(obj, function(v) { return v * 10; });
// → { a: 10, b: 20 }
Try It Yourself

How It Works

  1. mapKeys. Keys become uppercase; values stay.
  2. mapValues. Values × 10; keys stay.
  3. Pair. Chain both when you need to reshape both sides.
  4. Pick. Choose the helper that matches what must change.

Browser & Runtime Support

_.mapKeys is a Lodash Object helper since 3.8.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 3.8.0+

_.mapKeys()

Available in [email protected] full builds and the lodash/mapKeys modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 3.8+
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
_.mapKeys Lodash 3.8.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import mapKeys from "lodash/mapKeys" for smaller bundles. Use it to rename or normalize object keys without mutating the source.

Frequently Asked Questions

_.mapKeys(object, [iteratee]) creates a new object with the same values and keys produced by iteratee. Official: _.mapKeys({a:1,b:2}, (v,k) => k+v) → {a1:1, b2:2}.
It is called as (value, key, object) for each own enumerable string-keyed property. Whatever it returns becomes the new key.
No. It always returns a new object. The source is left unchanged.
Last write wins silently. The later property overwrites the earlier one at that key. Keep transformed keys unique.
mapKeys rewrites keys and keeps values. mapValues keeps keys and rewrites values.
Use import mapKeys from "lodash/mapKeys" or require("lodash/mapKeys") for tree-shaking. Full build: import _ from "lodash" then _.mapKeys.

Next: _.mapValues()

Transform each value while keeping the same keys.

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