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)
Call
What 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
Parameter
Description
obj
Required. 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 Value
Description
Object
A 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.
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.
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.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.