Lodash _.invert() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Creates an object composed of the inverted keys and values of object. Part of Lodash Object (stable — not deprecated). If values collide, later keys overwrite earlier ones. To keep every original key under a shared value, use _.invertBy.

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.invert. Start with this line.

Lodash
_.invert(object)
CallWhat it does
_.invert(object)Primary signature.

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

Library declarations
Lodash
_.invert(object)

Parameters

ParameterDescription
objectRequired. Object to invert

Return Value

Return ValueDescription
*New inverted object

Lodash Examples

Example 1 — Official: Invert with Duplicate Values

Later keys overwrite earlier ones for the same value.

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

console.log(_.invert(object));
// { '1': 'c', '2': 'b' }
Try It Yourself

How It Works

  1. Swap. Values become keys; keys become values.
  2. Collision. Both a and c map to 1.
  3. Last wins. Inverted key '1' keeps 'c'.
  4. Docs. Matches the official Lodash example.

Example 2 — Unique Values Round-Trip Cleanly

When values are unique, invert is a clean swap.

Lodash
var roles = { admin: "A", editor: "E", viewer: "V" };
var inverted = _.invert(roles);

console.log(inverted);
// { A: 'admin', E: 'editor', V: 'viewer' }
console.log(_.invert(inverted).admin); // 'A'
Try It Yourself

How It Works

  1. Unique. Each value appears once — no overwrites.
  2. Invert. Codes become keys; role names become values.
  3. Round-trip. Inverting again restores the original mapping.
  4. Use. Best when values are guaranteed unique.

Example 3 — Build a Reverse Lookup Table

Turn “name → code” into “code → name.”

Lodash
var status = { ok: 200, notFound: 404, error: 500 };
var byCode = _.invert(status);

console.log(byCode["404"]); // 'notFound'
console.log(byCode["200"]); // 'ok'
Try It Yourself

How It Works

  1. Source. Names map to status codes.
  2. Invert. Codes become string keys.
  3. Lookup. Read byCode['404'] to get the name.
  4. Tip. Access with string keys — object keys are strings.

Example 4 — Values Become String Keys

Non-string values are coerced when they become keys.

Lodash
var flags = { yes: true, no: false };

console.log(_.invert(flags));
// { true: 'yes', false: 'no' }
Try It Yourself

How It Works

  1. Booleans. Become keys 'true' / 'false'.
  2. Original keys. Stored as the new values.
  3. Warning. Objects become '[object Object]' and collide.
  4. Rule. Prefer primitive, unique values before inverting.

Example 5 — _.invert vs _.invertBy

Choose last-wins vs keep-all-keys.

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

console.log(_.invert(object));
// { '1': 'c', '2': 'b' }

console.log(_.invertBy(object));
// { '1': ['a', 'c'], '2': ['b'] }
Try It Yourself

How It Works

  1. invert. One key per value — last wins.
  2. invertBy. Groups every original key into an array.
  3. When. Unique values → invert.
  4. When. Duplicates matter → invertBy.

Browser & Runtime Support

_.invert is a Lodash Object helper available in 4.x (and earlier builds). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native equivalents loop own keys and assign result[value] = key.

✓ Stable · Lodash 4.x

_.invert()

Available in [email protected] full builds and the lodash/invert 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
_.invert Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import invert from "lodash/invert" for smaller builds. Use invertBy when duplicate values must be preserved.

Frequently Asked Questions

_.invert(object) creates a new object composed of the inverted keys and values. Official: _.invert({ a: 1, b: 2, c: 1 }) → { '1': 'c', '2': 'b' }.
Subsequent values overwrite earlier ones. In the official example both a and c map to 1, so the inverted key '1' keeps the last key, 'c'.
No. It returns a new object. The original is left unchanged.
Values become keys, so they are coerced to strings. Numbers become '1', booleans become 'true', and plain objects become '[object Object]'.
Use _.invertBy when values are not unique and you want every original key kept in an array under that inverted value.
Use import invert from "lodash/invert" or require("lodash/invert") so bundlers can tree-shake. Full build: import _ from "lodash" then _.invert.

Next: _.invertBy()

Invert an object and group original keys under each value.

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