Ramda.js R.lensIndex() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.lensIndex() answers a list question: how do I get and set one slot without rewriting the rest of the array every time? You pass an index. You get a lens. Then R.view reads that slot, R.set replaces it, and R.over maps a function over it. The original array is not mutated.

The official sample is the whole idea. R.lensIndex(0) focuses the first item of ['a', 'b', 'c']. View returns 'a'. Set returns ['x', 'b', 'c']. Over with R.toUpper returns ['A', 'b', 'c']. If the index is missing, set and over give you the original list back. Need a property name instead of an index? That is R.lensProp. Need a nested path? That is the next helper, R.lensPath. Need a custom getter and setter? That is the previous helper, R.lens.

Syntax

Call R.lensIndex. Start with this line.

Ramda
R.lensIndex(n)
CallWhat it does
R.lensIndex(n)Primary signature.
R.lensIndex(...)Call the object helper.

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

Library declarations
Ramda
R.lensIndex(n)

Parameters

ParameterDescription
nRequired. Number. The list index to focus. 0 is the first item. Negative indexes count from the end, like R.nth.

Return Value

Return ValueDescription
LensA focus you pass to R.view, R.set, and R.over. You rarely call the lens function yourself.

Ramda Examples

📚 Getting Started

The official head lens with view, set, and over, then the same focus written as R.lens.

Example 1 — Official Head Lens

Read index 0, replace it, map it with toUpper. The other items stay put.

Ramda
const headLens = R.lensIndex(0);

R.view(headLens, ['a', 'b', 'c']);
//=> 'a'
R.set(headLens, 'x', ['a', 'b', 'c']);
//=> ['x', 'b', 'c']
R.over(headLens, R.toUpper, ['a', 'b', 'c']);
//=> ['A', 'b', 'c']
Try It Yourself

How It Works

Index 0 is the first slot. That is the docs sample from ramdajs.com.

Example 2 — vs R.lens

Both views return the same first item.

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

R.view(R.lensIndex(0), items);
//=> 'a'
R.view(R.lens(R.nth(0), R.update(0)), items);
//=> 'a'
Try It Yourself

How It Works

R.lens is the constructor. lensIndex(0) is that pair already wired. Keep lens when the getter or setter is not nth plus update.

📈 Practical Patterns

Missing indexes, a last-item lens, then a curried over on the middle slot.

Example 3 — Out of Range

When the index is missing, set and over return the original list.

Ramda
R.set(R.lensIndex(3), 'x', ['a', 'b', 'c']);
//=> ['a', 'b', 'c']
R.over(R.lensIndex(-4), R.toUpper, ['a', 'b', 'c']);
//=> ['a', 'b', 'c']
Try It Yourself

How It Works

The docs: when idx < -list.length or idx >= list.length, set and over return the original list. Index 3 is past the end. Index -4 is too far from the end.

Example 4 — Last Item with -1

Negative indexes count from the end. The source array stays put.

Ramda
const lastLens = R.lensIndex(-1);
const items = ['a', 'b', 'c'];

R.view(lastLens, items);
//=> 'c'
R.set(lastLens, 'z', items);
//=> ['a', 'b', 'z']
items;
//=> ['a', 'b', 'c']
Try It Yourself

How It Works

R.nth(-1) is the last item. R.update(-1) writes a new list. That is the same pair lensIndex uses.

Example 5 — over with Curry

Uppercase the middle slot, then read it. Both helpers are data-last.

Ramda
const midLens = R.lensIndex(1);
const shoutMid = R.over(midLens, R.toUpper);
const readMid = R.view(midLens);
const items = ['a', 'b', 'c'];

shoutMid(items);
//=> ['a', 'B', 'c']
readMid(shoutMid(items));
//=> 'B'
Try It Yourself

How It Works

R.over(midLens, R.toUpper) waits for the list. R.view(midLens) waits for the list. Drop them into R.pipe when you need a longer chain.

Browser Support

R.lensIndex() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.14.0+ · Any JS runtime

Ramda.js R.lensIndex()

Load Ramda 0.14.0 or later via CDN or npm, then call R.lensIndex with a number. No extra browser API is required.

100% With Ramda 0.14.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.lensIndex() Universal

Bottom line: Safe in any project that includes Ramda 0.14.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.lensIndex(n) returns a lens whose focus is index n on a list. R.view reads that slot. R.set replaces it. R.over maps a function over it. Added in v0.14.0. The original array is not mutated.
const headLens = R.lensIndex(0). R.view(headLens, ['a', 'b', 'c']) is 'a'. R.set(headLens, 'x', ['a', 'b', 'c']) is ['x', 'b', 'c']. R.over(headLens, R.toUpper, ['a', 'b', 'c']) is ['A', 'b', 'c'].
When idx < -list.length or idx >= list.length, R.set and R.over return the original list. R.view of a missing index is undefined, the same as R.nth.
lensIndex(n) is R.lens with getter nth(n) and setter update(n). nth only reads. lens is the general constructor. Use lensIndex when the focus is one list slot.
Yes. R.lensIndex(-1) focuses the last item, matching R.nth(-1) and R.update(-1). Too far negative, such as -4 on a three-item list, is out of range.
No. R.lensIndex is a stable Ramda object helper from v0.14.0. It is not deprecated, experimental, or non-standard.

Continue to R.lensPath()

Learn how to focus nested keys using Ramda R.lensPath().

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