Ramda’s R.mapObjIndexed() answers an object-map question: how do I transform every own value when I also need its key? You pass a function of (value, key, obj), then an object. You get a new object with the same keys and new values. The original is not mutated.
The official sample is the whole idea. {x: 1, y: 2, z: 3} becomes {x: 'x2', y: 'y4', z: 'z6'} because the callback prefixes the key and doubles the number. Need only the value? That is R.map. Need side effects and the same object back? That is R.forEachObjIndexed. Need new key names? That is the previous helper, R.mapKeys.
Syntax
Call R.mapObjIndexed. Start with this line.
Ramda
R.mapObjIndexed(fn, obj)
Call
What it does
R.mapObjIndexed(fn, obj)
Primary signature.
R.mapObjIndexed(...)
Call the object helper.
Prefer import { mapObjIndexed } from "ramda" when you want a named import.
Library declarations
Ramda
R.mapObjIndexed(fn, obj)
Parameters
Parameter
Description
fn
Required. Function. Called as fn(value, key, obj) for each own key. The return value becomes the new field.
obj
Required. Object. The record to map. Own enumerable string keys only. Prototype names are skipped.
Return Value
Return Value
Description
Object
A new object with the same own keys and mapped values. The source object is not mutated.
Ramda Examples
📚 Getting Started
The official key-plus-double sample, then the same numbers with map that cannot see the key.
Example 1 — Official xyz
Prefix the key, double the number. Keys stay x, y, and z.
key is unused here; obj is the reason to pick mapObjIndexed over map. You could also compute the total once outside the map for a slightly cheaper pass.
Example 5 — Curry with pipe
Label each value with its key, then list the labels. Both helpers are data-last.
R.mapObjIndexed(fn) waits for the object. Drop it at the start of R.pipe when a later step needs the mapped values.
Compatibility
Browser Support
R.mapObjIndexed() 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.mapObjIndexed()
Load Ramda 0.9.0 or later via CDN or npm, then call R.mapObjIndexed with a callback and an object. 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.mapObjIndexed()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.mapObjIndexed(fn, obj) returns a new object with the same own keys. Each value is fn(value, key, obj). Added in v0.9.0. The original object is not mutated.
R.map only passes the value. Use map when the key does not matter. mapObjIndexed passes value, key, and the whole object. The docs say: if only the value is significant, use map instead.
forEachObjIndexed is for side effects and returns the original object. mapObjIndexed builds a new object of mapped values. mapKeys rewrites key names and copies values.
No. It walks R.keys(obj), so only own enumerable string keys. Inherited names and symbol keys are skipped.
No. R.mapObjIndexed is a stable Ramda object helper from v0.9.0. It is not deprecated, experimental, or non-standard.