Ramda.js R.modifyPath() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.modifyPath() answers a nested-update question: how do I change a deep field using its current value, without mutating the original object? You pass a path array, a function of that value, then the object. You get a clone along that path. The original is not mutated.

The official sample is the whole idea. R.reverse turns nested zip '90216' into '61209'. The same path can include an array index. Need only a top-level key? That is R.modify. Need a value you already know, and missing nodes created? That is R.assocPath.

Syntax

Call R.modifyPath. Start with this line.

Ramda
R.modifyPath(path, fn, object)
CallWhat it does
R.modifyPath(path, fn, object)Primary signature.
R.modifyPath(...)Call the object helper.

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

Library declarations
Ramda
R.modifyPath(path, fn, object)

Parameters

ParameterDescription
pathRequired. Array. Keys and indexes to walk, such as ['address', 'zipCode'] or ['addresses', 0, 'zipCode'].
fnRequired. Function. Called as fn(currentValue) when every step of the path exists.
objectRequired. Object or Array. The record to clone along the path. A missing step skips fn and returns this value unchanged.

Return Value

Return ValueDescription
ObjectA clone with fn stored at the path tail. Nodes off the path are copied by reference. The original is not mutated. A missing path returns the original object.

Ramda Examples

📚 Getting Started

The official nested reverse, then the same idea through an array index.

Example 1 — Official Nested Zip Code

R.reverse turns '90216' into '61209'. The original person stays put.

Ramda
const person = { name: 'James', address: { zipCode: '90216' } };

R.modifyPath(['address', 'zipCode'], R.reverse, person);
//=> { name: 'James', address: { zipCode: '61209' } }
person;
//=> { name: 'James', address: { zipCode: '90216' } }
Try It Yourself

How It Works

The path walks address, then applies R.reverse to the string. That is the sample from ramdajs.com.

Example 2 — Official Array Index Path

Integer 0 in the path is an array index. The nested zip still reverses.

Ramda
const person = { name: 'James', addresses: [{ zipCode: '90216' }] };

R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person);
//=> { name: 'James', addresses: [{ zipCode: '61209' }] }
Try It Yourself

How It Works

The helper clones the outer object, the addresses array, and the first item, then stores the reversed string. That is the second docs sample.

📈 Practical Patterns

Cousins, missing and empty paths, then a curried helper in a pipe.

Example 3 — vs R.modify and R.assocPath

Top-level modify never sees nested zipCode. assocPath writes a literal instead of transforming.

Ramda
const person = { name: 'James', address: { zipCode: '90216' } };

R.modify('zipCode', R.reverse, person);
//=> { name: 'James', address: { zipCode: '90216' } }
R.modifyPath(['address', 'zipCode'], R.reverse, person);
//=> { name: 'James', address: { zipCode: '61209' } }
R.assocPath(['address', 'zipCode'], '00000', person);
//=> { name: 'James', address: { zipCode: '00000' } }
Try It Yourself

How It Works

modify('zipCode', ...) looks for a missing top-level key, so it returns the original object. assocPath never reads '90216'.

Example 4 — Missing Path, Empty Path, Shared Refs

A missing nested key returns the same object. An empty path applies fn to the whole value. Unchanged siblings stay shared.

Ramda
const person = { name: 'James', address: { zipCode: '90216' }, tags: ['js'] };
const missing = R.modifyPath(['address', 'country'], R.toUpper, person);
const flipped = R.modifyPath(['address', 'zipCode'], R.reverse, person);

missing === person;
//=> true
R.modifyPath([], R.assoc('ok', true), { a: 1 });
//=> { a: 1, ok: true }
flipped.tags === person.tags;
//=> true
Try It Yourself

How It Works

The docs say the function is not invoked when the path does not exist. An empty path is the whole value. Mutating flipped.tags would also change person.tags.

Example 5 — Curry with pipe

Fix the reverseZip helper first, then read the nested zip code.

Ramda
const reverseZip = R.modifyPath(['address', 'zipCode'], R.reverse);
const zipAfter = R.pipe(
  R.modifyPath(['address', 'zipCode'], R.reverse),
  R.path(['address', 'zipCode'])
);
const person = { name: 'James', address: { zipCode: '90216' } };

reverseZip(person);
//=> { name: 'James', address: { zipCode: '61209' } }
zipAfter(person);
//=> '61209'
Try It Yourself

How It Works

R.modifyPath(['address', 'zipCode'], R.reverse) waits for one object, which is why it drops into R.pipe.

Browser Support

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

✓ Ramda 0.28.0+ · Any JS runtime

Ramda.js R.modifyPath()

Load Ramda 0.28.0 or later via CDN or npm, then call R.modifyPath with a path, a function, and an object. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.28.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.modifyPath(path, fn, object) returns a shallow clone where fn is applied to the value at path. Nodes along the path are copied. Other nested values are copied by reference. Added in v0.28.0. The original object is not mutated.
person is {name: 'James', address: { zipCode: '90216' }}. R.modifyPath(['address', 'zipCode'], R.reverse, person) is {name: 'James', address: { zipCode: '61209' }}. With addresses: [{ zipCode: '90216' }], the path ['addresses', 0, 'zipCode'] also yields zipCode '61209'.
modify updates one top-level property. modifyPath walks an array of keys, including array indexes. R.modify('zipCode', R.reverse, person) does nothing when zipCode lives under address.
assocPath writes a value you already know and creates missing nodes. modifyPath reads the current value, runs a function on it, and returns the original object if any step of the path is missing.
If any key along the path is missing, fn is not called and the original object is returned. An empty path applies fn to the whole object or array.
No. R.modifyPath is a stable Ramda object helper from v0.28.0. It is not deprecated, experimental, or non-standard.

Continue to R.objOf()

Learn how to wrap one key and one value as an object using Ramda R.objOf().

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