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)
Call
What 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
Parameter
Description
obj
Required. 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 Value
Description
Object
A 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.
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.
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.
Arity 1 makes invertObj a clean first step in a pipe. R.prop('alice') then reads the string. Missing names yield undefined.
Compatibility
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 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.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.