Ramda.js R.forEachObjIndexed() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

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)
CallWhat 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

ParameterDescription
fnRequired. Function. Called as (value, key, obj). The return value is ignored. Use it for side effects.
objRequired. Object. The record to walk. Own enumerable string keys only. null and undefined throw inside R.keys.

Return Value

Return ValueDescription
ObjectThe 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
const printKeyConcatValue = (value, key) => console.log(key + ':' + value);

R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2});
//=> {x: 1, y: 2}
// logs x:1
// logs y:2
Try It Yourself

How It Works

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.

Ramda
const obj = {x: 1, y: 2};
const seen = [];
let thirdIsObj = true;
const result = R.forEachObjIndexed((value, key, o) => {
  seen.push(key);
  thirdIsObj = thirdIsObj && o === obj;
}, obj);

result === obj;
//=> true

thirdIsObj;
//=> true

seen;
//=> ['x', 'y']

result;
//=> {x: 1, y: 2}
Try It Yourself

How It Works

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.

Ramda
const proto = { inherited: 9 };
const child = Object.create(proto);
child.x = 1;
child.y = 2;

R.forEachObjIndexed((value, key) => console.log(key + ':' + value), child);
// logs x:1
// logs y:2

R.keys(child);
//=> ['x', 'y']
Try It Yourself

How It Works

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.

Ramda
R.forEach((value) => console.log(value), [1, 2]);
// logs 1
// logs 2

R.forEachObjIndexed((value, key) => console.log(key + ':' + value), {a: 1, b: 2});
// logs a:1
// logs b:2
Try It Yourself

How It Works

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.

Ramda
const logPairs = R.forEachObjIndexed((value, key) => {
  console.log(key + '=' + value);
});
const rec = {n: 3, m: 4};

R.tap(logPairs, rec);
//=> {n: 3, m: 4}
// logs n=3
// logs m=4

logPairs(rec) === rec;
//=> true
Try It Yourself

How It Works

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.

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 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.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.

Continue to R.has()

Learn how to test whether an object owns a named key using Ramda R.has().

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