Underscore.js _.invert() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object’s values should be unique and string serializable (official Underscore docs).

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

Syntax

Call _.invert. Start with this line.

Underscore
_.invert(object)
CallWhat it does
_.invert(object)Primary signature.
_.invert(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.invert(object)

Parameters

ParameterDescription
objectRequired. Source map to flip

Return Value

Return ValueDescription
*New object (keys ↔ values)

Underscore.js Examples

Example 1 — Official: Flip Names

Swap each key with its string value.

Underscore
_.invert({ Moe: "Moses", Larry: "Louis", Curly: "Jerome" });
// → { Moses: "Moe", Louis: "Larry", Jerome: "Curly" }
Try It Yourself

How It Works

  1. Values. "Moses", "Louis", "Jerome" become keys.
  2. Keys. "Moe", "Larry", "Curly" become values.
  3. Unique. Each nickname is unique — no collisions.
  4. Copy. Source object is unchanged.

Example 2 — Numbers Become String Keys

Object keys are always strings after the flip.

Underscore
_.invert({ a: 1, b: 2 });
// → { "1": "a", "2": "b" }
Try It Yourself

How It Works

  1. Coerce. 1 and 2 become keys "1" and "2".
  2. Values. Original keys "a" and "b" are stored as values.
  3. Lookup. Use flipped["1"] (string), not only number 1.
  4. Docs tip. Values should be string-serializable.

Example 3 — Duplicate Values (Last Wins)

Shared values collide when they become keys.

Underscore
_.invert({ a: "x", b: "y", c: "x" });
// → { x: "c", y: "b" }
Try It Yourself

How It Works

  1. First x. a: "x" sets { x: "a" }.
  2. y. b: "y" adds y: "b".
  3. Second x. c: "x" overwrites → x: "c".
  4. Lesson. Keep values unique before inverting.

Example 4 — Reverse Lookup

Turn an id→label map into label→id for quick reverse searches.

Underscore
var byId = { 1: "admin", 2: "editor", 3: "viewer" };
var byLabel = _.invert(byId);

byLabel["editor"];
// → "2"
Try It Yourself

How It Works

  1. Forward. byId maps id → role label.
  2. Invert. Labels become keys; ids become string values.
  3. Lookup. byLabel["editor"] returns "2".
  4. Ids as keys. Original numeric keys were already stringified on the object.

Browser & Runtime Support

_.invert ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.invert()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.invert(object) returns a new object with keys and values swapped. Each value becomes a key and its original key becomes the value.
Those values collide as keys. The last one processed wins — only one entry survives for that value.
Yes. Object keys are strings, so values are coerced when they become keys — e.g. 1 becomes "1".
No. It returns a new object and leaves the input unchanged.
No. Only own enumerable properties are flipped.
Building a reverse lookup — for example name→id from an id→name map — when values are unique and string-friendly.

Next: _.create()

Create a new object that inherits from a given prototype.

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