Ramda.js R.addIndex() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

R.addIndex(fn) wraps a list iterator so its callback also receives the current index and the whole list. That turns R.map into something closer to Array.prototype.map.

Stable: R.addIndex has been in Ramda since v0.15.0. It is not deprecated, experimental, or non-standard.

Syntax

Call R.addIndex. Start with this line.

Ramda
R.addIndex(fn)
CallWhat it does
R.addIndex(fn)Primary signature.
R.addIndex(...)Call the function helper.

Prefer import { addIndex } from "ramda" when you want a named import.

Library declarations
Ramda
R.addIndex(fn)

Parameters

ParameterDescription
fnRequired. Iterator to wrap (e.g. R.map)
CallbackRequired. Gets original args + index + list
ListRequired. Last argument of the wrapped iterator

Return Value

Return ValueDescription
*New curried iterator (original unchanged)

Ramda Examples

Example 1 — Official mapIndexed

Wrap R.map once, then prefix every value with its index.

Ramda
const mapIndexed = R.addIndex(R.map);

console.log(
  mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r'])
);
// → ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
Try It Yourself

How It Works

  1. Wrap once. R.addIndex(R.map) returns a new map that can pass an index.
  2. Walk left to right. Each letter gets idx starting at 0.
  3. Build labels. idx + '-' + val turns 'f' into '0-f'.
  4. Result. A new array of six labeled strings; the original list is unchanged.

Example 2 — Plain R.map Has No Index

A second parameter on a Ramda map callback is not the index until you wrap.

Ramda
const letters = ['a', 'b', 'c'];

console.log(R.map((val, idx) => idx, letters));
// → [undefined, undefined, undefined]

console.log(R.addIndex(R.map)((val, idx) => idx, letters));
// → [0, 1, 2]
Try It Yourself

How It Works

  1. Native habit. Array.map passes (value, index), so beginners expect the same from R.map.
  2. Ramda rule. Plain R.map calls the callback with the value only — idx stays undefined.
  3. Fix. Wrap with R.addIndex(R.map) so the index is appended after the value.
  4. Check. The second call returns real indexes 0, 1, 2.

Example 3 — Filter Even Indexes

The same wrapper works on R.filter. Keep items whose position is even.

Ramda
const filterIndexed = R.addIndex(R.filter);

console.log(
  filterIndexed((val, idx) => idx % 2 === 0, ['a', 'b', 'c', 'd', 'e'])
);
// → ['a', 'c', 'e']
Try It Yourself

How It Works

  1. Wrap filter. R.addIndex(R.filter) adds idx to the predicate.
  2. Test position. idx % 2 === 0 keeps even slots.
  3. Keep. Indexes 0, 2, 4 → 'a', 'c', 'e'.
  4. Drop. 'b' and 'd' sit on odd indexes and are removed.

Example 4 — Reduce with Index

After wrapping, the iteratee is (acc, value, index, list).

Ramda
const reduceIndexed = R.addIndex(R.reduce);

console.log(
  reduceIndexed(
    (acc, val, idx) => acc.concat(idx + ':' + val),
    [],
    ['a', 'b', 'c']
  )
);
// → ['0:a', '1:b', '2:c']
Try It Yourself

How It Works

  1. Wrap reduce. Extra args are appended after acc and val.
  2. Start empty. Accumulator begins as [].
  3. Append labels. Each step concatenates idx + ':' + val.
  4. Finish. Three labeled strings in one new array.

Example 5 — Mark the Last Item

The third extra argument is the whole list — use it for first/last checks.

Ramda
const mapIndexed = R.addIndex(R.map);
const items = ['alpha', 'beta', 'gamma'];

console.log(
  mapIndexed((val, idx, list) => {
    return idx === list.length - 1 ? val + ' (last)' : val;
  }, items)
);
// → ['alpha', 'beta', 'gamma (last)']
Try It Yourself

How It Works

  1. Receive the list. The wrapper passes list after idx.
  2. Compare. idx === list.length - 1 is true only for the final item.
  3. Suffix. Only 'gamma' becomes 'gamma (last)'.
  4. Ignore when unused. If you only need the index, write (val, idx) and skip list.

Browser & Runtime Support

R.addIndex is a Ramda Function helper since v0.15.0. It is plain JavaScript — no special browser API. It runs anywhere Ramda loads (CDN, npm, bundler).

✓ Stable · Ramda 0.15.0+

R.addIndex()

Available in [email protected]+ full builds. Works in evergreen browsers and current Node LTS / Bun / Deno when Ramda is installed.

100% With Ramda 0.15+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.addIndex Ramda 0.15.0+

Bottom line: Safe for production once Ramda 0.15.0+ is loaded. Use a CDN build such as ramda.min.js in the browser, or npm install ramda for Node. No polyfill is needed beyond shipping Ramda itself.

Frequently Asked Questions

R.addIndex(fn) takes a list iterator such as R.map, R.filter, or R.reduce and returns a new iterator whose callback also receives the current index and the whole list. That makes Ramda's R.map closer to Array.prototype.map.
const mapIndexed = R.addIndex(R.map); then mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']) returns ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']. Added in v0.15.0.
Ramda iterators pass only the current value by design, which keeps them simple and point-free. When you need the index, wrap the iterator with R.addIndex instead of switching to a native array method.
It works when the iteration callback is the first parameter and the list is the last parameter. That matches R.map, R.filter, R.forEach, and R.reduce. It will not magically fix a function with a different argument order.
Both add (value, index, list) to the callback. R.addIndex walks left to right (index 0 first). R.addIndexRight walks right to left, so indexes count from the end.
No. R.addIndex is a stable Ramda Function helper from v0.15.0. It is not deprecated, experimental, or non-standard.

About the author

Next: R.addIndexRight()

Count indexes from the end of the list, and see why the official sample reverses first.

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

12 people found this page helpful