Ramda.js R.lensPath() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.lensPath() answers a nested question: how do I get and set a value deep in an object without rewriting the rest of the tree every time? You pass a path array of keys and indexes. You get a lens. Then R.view reads that value, R.set replaces it, and R.over maps a function over it. The original structure is not mutated.

The official sample is the whole idea. R.lensPath(['x', 0, 'y']) walks into x, then the first array item, then y. View returns 2. Set returns a new tree with that y as 1. Over with R.negate returns -2 in the same slot. Need one own key? That is the next helper, R.lensProp. Need one list index? That is the previous helper, R.lensIndex. Need a one-off write with no view? That is R.assocPath.

Syntax

Call R.lensPath. Start with this line.

Ramda
R.lensPath(path)
CallWhat it does
R.lensPath(path)Primary signature.
R.lensPath(...)Call the object helper.

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

Library declarations
Ramda
R.lensPath(path)

Parameters

ParameterDescription
pathRequired. Array. The trail to focus. Each step is a string key, an integer index, or a symbol. Official: ['x', 0, 'y'].

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 mixed path with view, set, and over, then a one-key path next to lensProp.

Example 1 — Official Mixed Path

Walk x, then index 0, then y. The sibling z and the second item stay put.

Ramda
const xHeadYLens = R.lensPath(['x', 0, 'y']);
const data = {x: [{y: 2, z: 3}, {y: 4, z: 5}]};

R.view(xHeadYLens, data);
//=> 2
R.set(xHeadYLens, 1, data);
//=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
R.over(xHeadYLens, R.negate, data);
//=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
Try It Yourself

How It Works

The path mixes a key, an index, and another key. That is the docs sample from ramdajs.com.

Example 2 — vs R.lensProp

A one-key path matches lensProp. Nested trails still need lensPath.

Ramda
const user = {name: 'Ada', city: 'London'};

R.view(R.lensPath(['name']), user);
//=> 'Ada'
R.view(R.lensProp('name'), user);
//=> 'Ada'
Try It Yourself

How It Works

Prefer R.lensProp('name') when the focus is one own key. Keep lensPath when the trail is nested or mixed.

📈 Practical Patterns

The same trail as path and assocPath, a missing nest that set creates, then a curried over on age.

Example 3 — vs R.path and R.assocPath

view matches path. set matches assocPath.

Ramda
const trail = ['user', 'name'];
const data = {user: {name: 'Ada', age: 36}};

R.view(R.lensPath(trail), data);
//=> 'Ada'
R.path(trail, data);
//=> 'Ada'
R.set(R.lensPath(trail), 'Grace', data);
//=> {user: {name: 'Grace', age: 36}}
R.assocPath(trail, 'Grace', data);
//=> {user: {name: 'Grace', age: 36}}
Try It Yourself

How It Works

The source is that pair. Use path or assocPath for a one-off. Use lensPath when you also need over, or will reuse the focus.

Example 4 — Missing Path

view is undefined. set builds the nested objects. The original stays empty.

Ramda
const nameLens = R.lensPath(['user', 'name']);
const empty = {};

R.view(nameLens, empty);
//=> undefined
R.set(nameLens, 'Ada', empty);
//=> {user: {name: 'Ada'}}
empty;
//=> {}
Try It Yourself

How It Works

That is assocPath creating intermediate objects. R.lensIndex does the opposite on a missing index: set returns the original list. Do not expect both helpers to grow structures the same way.

Example 5 — over with Curry

Bump a nested age, then read it. Both helpers are data-last.

Ramda
const ageLens = R.lensPath(['user', 'age']);
const bumpAge = R.over(ageLens, R.inc);
const readAge = R.view(ageLens);
const data = {user: {name: 'Ada', age: 36}};

bumpAge(data);
//=> {user: {name: 'Ada', age: 37}}
readAge(bumpAge(data));
//=> 37
Try It Yourself

How It Works

R.over(ageLens, R.inc) waits for the object. R.view(ageLens) waits for the object. Drop them into R.pipe when you need a longer chain.

Browser Support

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

✓ Ramda 0.19.0+ · Any JS runtime

Ramda.js R.lensPath()

Load Ramda 0.19.0 or later via CDN or npm, then call R.lensPath with a path array. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.19.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.lensPath(path) returns a lens whose focus is a nested trail of keys and indexes. R.view reads that value. R.set replaces it. R.over maps a function over it. Added in v0.19.0. The original structure is not mutated.
const xHeadYLens = R.lensPath(['x', 0, 'y']). On {x: [{y: 2, z: 3}, {y: 4, z: 5}]}, view is 2, set 1 is {x: [{y: 1, z: 3}, {y: 4, z: 5}]}, and over negate is {x: [{y: -2, z: 3}, {y: 4, z: 5}]}.
lensProp focuses one own key. lensIndex focuses one list index. lensPath walks a trail that can mix both, such as ['x', 0, 'y']. A one-key path matches lensProp. A one-index path matches lensIndex.
R.view returns undefined, the same as R.path. R.set builds the nested objects with R.assocPath, so an empty {} can become {user: {name: 'Ada'}}. That is different from lensIndex, which does not grow a list.
The source is R.lens with getter path(p) and setter assocPath(p). view matches path. set matches assocPath. over maps, then assocPath writes the result back.
No. R.lensPath is a stable Ramda object helper from v0.19.0. It is not deprecated, experimental, or non-standard.

Continue to R.lensProp()

Learn how to focus get and set on one object key using Ramda R.lensProp().

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