Lodash _.mapValues() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.mapValues(object, [iteratee]) rewrites every value while keeping the same keys.
  • The iteratee signature (value, key, object) and the four shorthand forms.
  • How to use the property-name shorthand to pluck a field from each record in one line.
  • How _.mapValues pairs with _.mapKeys() for full object reshaping.

Prerequisites

Comfortable with Lodash iteratee shorthands. _.mapValues is essentially Array.prototype.map for plain objects.

  • Values, not keys: use _.mapKeys if you only want to transform keys.
  • Pure function: returns a new object; the source is never mutated.
  • Own enumerable only: inherited and symbol-keyed properties are skipped.

Overview

_.mapValues iterates the source’s own enumerable string-keyed properties, calls the iteratee with (value, key, object), and stores the return value under the original key. Use it for unit conversion, type coercion, sanitizing fields, plucking nested properties, or computing derived shapes—anywhere you want object-in, object-out.

map for objects

Like Array.prototype.map, but the input and output keep their object shape.

Shorthand pluck

Pass a property name string to extract a field: mapValues(users, "age").

Pure transform

Safe for selectors and memoized helpers—the input is never modified.

Syntax

javascript
_.mapValues(object, [iteratee=_.identity])
  • object: source to iterate over. Only own enumerable string-keyed properties are visited.
  • iteratee (optional): function, property name string, [path, value], or matcher object. Receives (value, key, object).
  • Returns: a new object with the same keys and iteratee-produced values.
1

Transform every value

The most common shape—feed a function, get a new object with the same keys and transformed values.

javascript
import mapValues from "lodash/mapValues";

const scores = { a: 1, b: 2, c: 3 };

mapValues(scores, (value) => value * 2);
// -> { a: 2, b: 4, c: 6 }

const fruits = { a: "apple", b: "banana", c: "cherry" };
mapValues(fruits, (value) => value.toUpperCase());
// -> { a: "APPLE", b: "BANANA", c: "CHERRY" }

scores;
// -> { a: 1, b: 2, c: 3 }
//    Source is unchanged.
Try it Yourself
2

Property shorthand: pluck a field

When values are nested objects, pass a property name string to extract that field from each record. The same Lodash shorthand grammar that _.map uses applies here.

javascript
import mapValues from "lodash/mapValues";

const employees = {
  e1: { name: "John",  age: 30, role: "admin" },
  e2: { name: "Alice", age: 25, role: "user"  },
  e3: { name: "Bob",   age: 35, role: "user"  }
};

mapValues(employees, "name");
// -> { e1: "John", e2: "Alice", e3: "Bob" }

mapValues(employees, "age");
// -> { e1: 30, e2: 25, e3: 35 }

// Matcher shorthand returns booleans:
mapValues(employees, { role: "admin" });
// -> { e1: true, e2: false, e3: false }

// Function form using (value, key, object):
mapValues(employees, (emp) => ({ ...emp, isSenior: emp.age > 30 }));
// -> { e1: { name: "John",  age: 30, role: "admin", isSenior: false },
//      e2: { name: "Alice", age: 25, role: "user",  isSenior: false },
//      e3: { name: "Bob",   age: 35, role: "user",  isSenior: true  } }
Try it Yourself
3

Sanitize fields & convert types

Real-world flow: trim incoming strings, coerce numerics, and use (value, key, object) for per-field logic.

javascript
import mapValues from "lodash/mapValues";

const userInput = {
  username: "   john_doe   ",
  email: "  john@example.com ",
  age: "30"
};

mapValues(userInput, (value) =>
  typeof value === "string" ? value.trim() : value
);
// -> { username: "john_doe", email: "john@example.com", age: "30" }

// Per-field logic via the (value, key, object) signature:
const sanitizers = {
  username: (s) => s.trim().toLowerCase(),
  email:    (s) => s.trim(),
  age:      (s) => Number(s)
};

mapValues(userInput, (value, key) => (sanitizers[key] || ((v) => v))(value));
// -> { username: "john_doe", email: "john@example.com", age: 30 }
//    age was coerced from "30" to 30.
Try it Yourself

📋 _.mapValues vs related helpers

Topic_.mapValues_.mapKeysArray.prototype.mapNative Object.fromEntries
InputObjectObjectArrayIterable of [k,v]
OutputObject (same keys)Object (transformed keys)ArrayObject
Iteratee args(value, key, object)(value, key, object)(value, index, array)([key, value])
Shorthand supportYesYesNo (write the map function)No
Mutates inputNoNoNoNo

Use _.mapValues when only values change. Use _.mapKeys when only keys change. For rewriting both at once, reach for Object.fromEntries(Object.entries(obj).map(...)).

Pitfalls to avoid

Mutation

Returning the same reference shares state

If your iteratee returns the same nested object it received (instead of a copy), the new map shares those nested objects with the source. Later mutations affect both.

Default

Default iteratee just clones

Without an iteratee, _.identity is used—you get a shallow clone of own enumerable values. Almost always pass an explicit iteratee.

Errors

Throws bubble up

If your iteratee throws, _.mapValues propagates the error. Wrap the body in try/catch if you need per-value resilience.

Iteration

Inherited and symbol keys are skipped

Only own enumerable string-keyed properties are processed. Bring symbols or prototype properties forward manually if you need them.

❓ FAQ

It is called with (value, key, object) for each own enumerable string-keyed property. Whatever it returns becomes the new value at that key.
No. It returns a new object with the same keys and transformed values. The original is untouched.
_.mapValues keeps keys and rewrites values. _.mapKeys keeps values and rewrites keys. Both iterate over own enumerable string-keyed properties and return a new object.
Function, property name string, [path, value] array, and object matcher. The property-name shorthand is especially handy when values are nested objects: _.mapValues(users, 'age') picks the age out of each record.
Array.map operates on arrays and produces an array; _.mapValues operates on plain objects and produces an object. Use the right tool for the right shape.
They are skipped. _.mapValues iterates only own enumerable string-keyed properties.

Summary

Did you know?

_.mapValues is the object-shaped Array.prototype.map. Keys are preserved exactly; only values are rewritten. Pair it with _.mapKeys when you need to rewrite both sides at once.

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.

5 people found this page helpful