Lodash _.invert() method

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

What you’ll learn

  • How _.invert(object) returns a new object with keys and values swapped.
  • Why duplicate values collide—and how iteration order decides the survivor.
  • How non-string values are coerced when they become keys.
  • When to reach for _.invertBy() instead to preserve duplicates.

Prerequisites

You should know that object keys are always strings (or symbols) in JavaScript. That single fact explains most of _.invert’s quirks.

  • One-to-one mappings only: values must be unique, or use _.invertBy.
  • Values become string keys: non-string values are coerced (e.g. numbers, booleans, objects).
  • Pure function: always returns a new object; the input is never mutated.

Overview

_.invert walks the source’s own enumerable string-keyed properties and writes a new { value: key } pair for each one. Because object keys can’t be objects, values are stringified along the way. When two source keys share a value, the later iteration overwrites the earlier one—that’s the single most common surprise.

Swap, don’t mutate

A new object comes back. The original stays exactly as it was.

Lookup tables in one line

Turn a { name: code } map into { code: name } for fast reverse lookups.

Last-write-wins

Duplicate values overwrite. Reach for _.invertBy when collisions matter.

Syntax

javascript
_.invert(object)
  • object: any object whose own enumerable string-keyed values you want as the new keys.
  • Returns: a brand-new object where the original values become keys and original keys become values.
1

Swap keys and values

The classic shape—a name-to-code map becomes a code-to-name map. Each lookup is now O(1) in either direction.

javascript
import invert from "lodash/invert";

const fruitColors = {
  apple: "red",
  banana: "yellow",
  grape: "purple"
};

const colorToFruit = invert(fruitColors);
// -> { red: "apple", yellow: "banana", purple: "grape" }

colorToFruit.yellow;
// -> "banana"

fruitColors;
// -> { apple: "red", banana: "yellow", grape: "purple" }
//    The original is untouched.
Try it Yourself
2

Duplicate values collide (last wins)

When two source keys share a value, the second one overwrites the first. If you need to keep every original key, use _.invertBy().

javascript
import invert from "lodash/invert";
import invertBy from "lodash/invertBy";

const scores = { A: 1, B: 2, C: 1 };

invert(scores);
// -> { "1": "C", "2": "B" }
//    A was overwritten by C because both map to 1.

invertBy(scores);
// -> { "1": ["A", "C"], "2": ["B"] }
//    invertBy preserves every source key.
Try it Yourself
3

Value coercion: numbers, booleans, objects

Every value is stringified when it becomes a key. That works fine for numbers, but turns every nested object into the string "[object Object]", which means all of them collide.

javascript
import invert from "lodash/invert";

const numericMap = { 1: "one", 2: "two", 3: "three" };
invert(numericMap);
// -> { one: "1", two: "2", three: "3" }
//    The original 1, 2, 3 keys were already strings in JS object keys.

const mixed = { a: 1, b: true, c: null };
invert(mixed);
// -> { "1": "a", "true": "b", "null": "c" }
//    All values were coerced to strings.

const nested = {
  user: { id: 1 },
  role: { id: 2 }
};
invert(nested);
// -> { "[object Object]": "role" }
//    Both nested objects stringify the same way and collide.
Try it Yourself

📋 _.invert vs related helpers

Topic_.invert_.invertByManual Object.fromEntries
ReturnsNew flat objectNew object of arraysNew flat object
Duplicate valuesLast wins (lossy)Grouped into arraysLast wins (lossy)
Value coercionTo stringTo stringTo string
Custom transformNoYes (iteratee per value)Yes (any map step)
Mutates inputNoNoNo

Use _.invert for one-to-one maps. Switch to _.invertBy the moment values can repeat or you need to bucket the keys. Native Object.fromEntries(Object.entries(obj).map(([k,v]) => [v,k])) is fine for small, controlled cases.

Pitfalls to avoid

Collisions

Silent data loss with duplicates

No warning is thrown when keys collide. Validate uniqueness up front or switch to _.invertBy.

Objects

Nested objects all stringify to "[object Object]"

Inverting a map of nested objects collapses every entry under the same key. Flatten or extract a primitive first.

Types

Numbers, booleans, and null become strings

After inverting, every key is a string. obj[1] still works because JS coerces, but TypeScript will treat the new key set as strings.

Order

Iteration order decides who wins

Don’t rely on which duplicate “wins” if your source comes from JSON.parse or merged inputs—treat the choice as undefined for your business logic.

❓ FAQ

No. _.invert returns a new object. The original is untouched.
Last write wins. The new object will keep the most recently iterated key under that value. Iteration order follows Lodash key iteration order (insertion order for string-keyed properties).
Every value is coerced to a string when it becomes a key. Numbers, booleans, null, and undefined are stringified; objects and arrays become things like "[object Object]" or "1,2,3".
Not in a useful way. Nested objects become the string "[object Object]", so all of them collide. Flatten or transform first.
No. _.invertBy groups the original keys into arrays per inverted value, which preserves duplicates. Use it whenever values are not unique.
It only walks own enumerable string-keyed properties. Symbol keys and inherited properties are ignored.

Summary

Did you know?

Object keys are always strings (or symbols), so _.invert stringifies every value when it becomes a key—true becomes "true" and { a: 1 } becomes "[object Object]". The original keys, however, are kept as-is for the new values.

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