Ramda’s R.forEachObjIndexed() answers a logging question: run this function once for every own key, then give me the same object back. You pass the callback, then the object. The callback receives the value, the key, and the object. The original record is returned so the helper can sit in a pipe.
The official sample is the whole idea. printKeyConcatValue logs x:1 and y:2, and the call itself is still {x: 1, y: 2}. Native Object.entries(obj).forEach(([key, value]) => …) returns undefined and leads with the key. List R.forEach is the array cousin: value only, original list back. Need new values? That is R.map (or R.mapObjIndexed). Need to peek inside a pipe without changing the value? Pair this helper with R.tap.
Syntax
Call R.forEachObjIndexed. Start with this line.
Ramda
R.forEachObjIndexed(fn, obj)
Call
What it does
R.forEachObjIndexed(fn, obj)
Primary signature.
R.forEachObjIndexed(...)
Call the object helper.
Prefer import { forEachObjIndexed } from "ramda" when you want a named import.
Library declarations
Ramda
R.forEachObjIndexed(fn, obj)
Parameters
Parameter
Description
fn
Required. Function. Called as (value, key, obj). The return value is ignored. Use it for side effects.
obj
Required. Object. The record to walk. Own enumerable string keys only. null and undefined throw inside R.keys.
Return Value
Return Value
Description
Object
The same obj reference you passed in. Nothing is copied. If fn mutates obj, that mutation is visible on the return value.
Ramda Examples
📚 Getting Started
The official Ramda sample, then proof that the return value is the original object.
Example 1 — Official Log Pairs
The callback logs key:value. The call still returns {x: 1, y: 2}.
Ramda walks x, then y. Each call is fn(value, key, obj), so the logger can lead with the key even though the first argument is the value. The return of console.log is ignored. That is the docs sample from ramdajs.com.
Example 2 — Same Object Back
The return value is the original reference. The third callback argument is that same object.
Native Array.prototype.forEach returns undefined, which is why it is awkward in a pipe. Ramda returns obj so the next function can keep working. The Try-it lab also checks that the third argument o is obj.
📈 Practical Patterns
Own keys versus prototypes, the list cousin forEach, then a curried helper inside tap.
Example 3 — Own Keys Only
Inherited prototype properties are not visited. The walk uses R.keys.
A for…in loop would also see inherited. forEachObjIndexed does not, because R.keys is own enumerable string keys only. That is safer for records created with Object.create.
Example 4 — vs R.forEach
List forEach logs values. Object forEachObjIndexed logs key:value. Both return the original collection.
R.forEach is for arrays (and array-likes). It does not pass an index unless you wrap it with R.addIndex. Objects always need the key, so they got a separate helper rather than overloading forEach.
Example 5 — Curry and R.tap
Fill the callback to get a function of one object. tap runs it and still returns the record.
Because forEachObjIndexed is arity 2, filling fn leaves a function of one object. R.tap already returns its value, so wrapping is optional — but tap makes the “peek, then continue” intent obvious in R.pipe.
Compatibility
Browser Support
R.forEachObjIndexed() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.23.0+ · Any JS runtime
Ramda.js R.forEachObjIndexed()
Load Ramda 0.23.0 or later via CDN or npm, then call R.forEachObjIndexed with a callback and an object. No extra browser API is required.
100%With Ramda 0.23.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.forEachObjIndexed()Universal
Bottom line: Safe in any project that includes Ramda 0.23.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.forEachObjIndexed(fn, obj) calls fn once for every own enumerable key, then returns the original object. fn receives (value, key, obj). Added in v0.23.0.
printKeyConcatValue logs key + ':' + value. R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}) logs x:1 then y:2, and the call itself is {x: 1, y: 2}.
No. It returns the same object reference you passed in. The return value of fn is ignored. Use it for logging, metrics, or other side effects.
R.forEach walks a list and the callback gets only the value. forEachObjIndexed walks an object and the callback gets value, key, and obj. R.map builds a new collection of transformed values.
No. It walks R.keys(obj), which is own enumerable string keys. Inherited properties are skipped. Symbol keys from Object.getOwnPropertySymbols are also skipped.
No. R.forEachObjIndexed is a stable Ramda object helper from v0.23.0. It is not deprecated, experimental, or non-standard.