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)
Call
What 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
Parameter
Description
n
Required. Number. The list index to focus. 0 is the first item. Negative indexes count from the end, like R.nth.
Return Value
Return Value
Description
Lens
A 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.
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.
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.
Compatibility
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 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.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.