Ramda.js R.invert() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.invert() answers a reverse lookup: which original keys share this value? You pass an object (or array). You get a new object. Each original value becomes a key. Each new value is an array of the keys that pointed at it.

The official sample is the whole idea. A race board maps placements to names. Alice finished first and third. Jake finished second. R.invert maps names back to placements: alice is ['first', 'third']. Native JavaScript has no one-liner for this. Need a single key per value, and you are fine dropping earlier duplicates? That is R.invertObj. Lodash has no direct invert-to-arrays helper; Ramda named this one so group-by-value stays obvious.

Syntax

Call R.invert. Start with this line.

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

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

Library declarations
Ramda
R.invert(obj)

Parameters

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

Return Value

Return ValueDescription
ObjectA new object. Each original value is a string key. Each value is an array of original keys, in encounter order. The source is not mutated.

Ramda Examples

📚 Getting Started

The official race sample, then the same idea compared with invertObj.

Example 1 — Official Race Results

alice appears twice, so invert collects both placements. jake maps to a one-item array.

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

How It Works

Keys are visited in object order: first, second, third. The second alice appends 'third' onto the same array. That is the docs sample from ramdajs.com.

Example 2 — vs R.invertObj

invert keeps a and c. invertObj keeps only the last key for 1.

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

How It Works

Number values become the string keys '1' and '2'. invertObj overwrites '1' when it sees c. Next Object helper is invertObj.

📈 Practical Patterns

Arrays, empty objects, coerced keys, then a pipe lookup.

Example 3 — Arrays and Empty Object

Array indexes become the collected keys. An empty object inverts to {}.

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

How It Works

Arrays are objects whose own keys are '0', '1', '2'. invert collects those index strings under each item. Empty input has no keys, so the output is empty.

Example 4 — String Keys and Immutability

Numbers and booleans become string keys. The original object stays the same.

Ramda
const orig = { x: 1, y: 1, z: true };
R.invert(orig);
//=> { '1': ['x', 'y'], true: ['z'] }
orig;
//=> { x: 1, y: 1, z: true }
Try It Yourself

How It Works

JavaScript object keys are strings. true becomes 'true'. invert writes a new object; orig is left alone.

Example 5 — Lookup with pipe

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

Ramda
const raceResultsByFirstName = {
  first: 'alice',
  second: 'jake',
  third: 'alice'
};
const byName = R.invert(raceResultsByFirstName);
byName.alice;
//=> ['first', 'third']

const jakePlaces = R.pipe(R.invert, R.prop('jake'));
jakePlaces(raceResultsByFirstName);
//=> ['second']
Try It Yourself

How It Works

Arity 1 makes invert a clean first step in a pipe. R.prop('jake') then reads the list. Missing names yield undefined, not an empty array.

Browser Support

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

Load Ramda 0.9.0 or later via CDN or npm, then call R.invert 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.invert() 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.invert(obj) returns a new object whose keys are the original values (coerced to strings) and whose values are arrays of the original keys. Duplicate values keep every key. Added in v0.9.0.
Given {first:'alice', second:'jake', third:'alice'}, R.invert returns {alice:['first','third'], jake:['second']}.
invert always stores original keys in arrays, so duplicates are kept. invertObj stores a single key and prefers the last key when the same value appears twice.
No. It builds a new object. The source keys and values stay in place.
Arrays invert too: indexes become the collected keys. Number and boolean values become string keys such as '1' and 'true' because object keys are strings.
No. R.invert is a stable Ramda object helper from v0.9.0. It is not deprecated, experimental, or non-standard.

Continue to R.invertObj()

Learn how to flip an object to a single key per value using Ramda R.invertObj().

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