Ramda.js R.invertObj() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.invertObj() answers a one-to-one reverse lookup: which original key holds this value? You pass an object (or array). You get a new object. Each original value becomes a key. Each new value is the original key as a string. If two fields share a value, the later key overwrites the earlier one.

The official samples are the whole idea. A race board maps first to alice and second to jake. invertObj maps names back to placements as strings. An array of names maps each name to its index string. Need every original key when a value repeats? That is the previous helper, R.invert. Underscore’s _.invert is this last-key-wins flip, not Ramda’s grouped invert.

Syntax

Call R.invertObj. Start with this line.

Ramda
R.invertObj(obj)
CallWhat it does
R.invertObj(obj)Primary signature.
R.invertObj(...)Call the object helper.

Prefer import { invertObj } from "ramda" when you want a named import.

Library declarations
Ramda
R.invertObj(obj)

Parameters

ParameterDescription
objRequired. Object or Array. The record to flip. Own enumerable keys are read with R.keys. Arrays work: indexes become the new string values.

Return Value

Return ValueDescription
ObjectA new object. Each original value is a string key. Each value is the last original key for that value. The source is not mutated.

Ramda Examples

📚 Getting Started

Both official samples, then last-key-wins compared with invert.

Example 1 — Official Race Results

Names become keys. Placements become strings. An array uses index strings as those values.

Ramda
const raceResults = {
  first: 'alice',
  second: 'jake'
};
R.invertObj(raceResults);
//=> { alice: 'first', jake: 'second' }

R.invertObj(['alice', 'jake']);
//=> { alice: '0', jake: '1' }
Try It Yourself

How It Works

Each value becomes a key. Each original key becomes a string value. Arrays are objects whose own keys are '0' and '1'. That is the docs sample from ramdajs.com.

Example 2 — Last Key Wins vs R.invert

When alice appears twice, invertObj keeps third. invert keeps both placements.

Ramda
const raceResults = {
  first: 'alice',
  second: 'jake',
  third: 'alice'
};
R.invertObj(raceResults);
//=> { alice: 'third', jake: 'second' }
R.invert(raceResults);
//=> { alice: ['first', 'third'], jake: ['second'] }
Try It Yourself

How It Works

The assignment out[obj[key]] = key overwrites alice when third is visited. R.invert appends instead. That is the docs note: the last key found is preferred.

📈 Practical Patterns

Empty objects, coerced numbers, colliding 1 and '1', then a pipe lookup.

Example 3 — Empty Object and Numbers

Empty input stays empty. Number values become string keys. The original object is unchanged.

Ramda
R.invertObj({});
//=> {}

const orig = { a: 1, b: 2 };
R.invertObj(orig);
//=> { '1': 'a', '2': 'b' }
orig;
//=> { a: 1, b: 2 }
Try It Yourself

How It Works

JavaScript object keys are strings, so 1 is stored as '1'. invertObj writes a new object; orig is left alone.

Example 4 — 1 vs '1'

A number and a string that look the same become one key. The later original key wins.

Ramda
R.invertObj({ x: 1, y: '1' });
//=> { '1': 'y' }
Try It Yourself

How It Works

First out[1] = 'x', then out['1'] = 'y'. Those are the same property. x is gone. invert would have stored ['x', 'y'] under '1'.

Example 5 — Lookup with pipe

Invert once, then read a name. Or compose invertObj and prop in one pipeline.

Ramda
const raceResults = {
  first: 'alice',
  second: 'jake'
};
const byName = R.invertObj(raceResults);
byName.alice;
//=> 'first'

const alicePlace = R.pipe(R.invertObj, R.prop('alice'));
alicePlace(raceResults);
//=> 'first'
Try It Yourself

How It Works

Arity 1 makes invertObj a clean first step in a pipe. R.prop('alice') then reads the string. Missing names yield undefined.

Browser Support

R.invertObj() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.9.0+ · Any JS runtime

Ramda.js R.invertObj()

Load Ramda 0.9.0 or later via CDN or npm, then call R.invertObj with an object or array. No extra browser API is required.

100% With Ramda 0.9.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.invertObj() Universal

Bottom line: Safe in any project that includes Ramda 0.9.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].

Frequently Asked Questions

R.invertObj(obj) returns a new object whose keys are the original values (coerced to strings) and whose values are the original keys as strings. If two fields share a value, the last key wins. Added in v0.9.0.
R.invertObj({first:'alice', second:'jake'}) is {alice:'first', jake:'second'}. R.invertObj(['alice','jake']) is {alice:'0', jake:'1'}.
invertObj stores one original key per value. invert stores every original key in an array. Use invert when duplicates must not be dropped.
No. It builds a new object. The source keys and values stay in place.
Arrays invert too: indexes become the new values. Number 1 and string '1' become the same key, so the later original key overwrites the earlier one.
No. R.invertObj is a stable Ramda object helper from v0.9.0. It is not deprecated, experimental, or non-standard.

Continue to R.keys()

Learn how to list an object’s own enumerable keys using Ramda R.keys().

R.keys() 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.

6 people found this page helpful