Ramda.js R.modify() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.modify() answers a one-field update question: how do I change a property using its current value, without mutating the original object? You pass the property name, a function of that value, then the object. You get a shallow copy. The original is not mutated.

The official sample is the whole idea. R.add(1) turns age 20 into 21. R.append('turtle') grows the pets list. Need a value you already know? That is R.assoc. Need several keys, including nested ones? That is R.evolve. Need a nested path? Next is R.modifyPath.

Syntax

Call R.modify. Start with this line.

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

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

Library declarations
Ramda
R.modify(prop, fn, object)

Parameters

ParameterDescription
propRequired. String or Number. The property to update. Integer keys also work on arrays.
fnRequired. Function. Called as fn(currentValue) when the property exists.
objectRequired. Object or Array. The record to copy. Missing properties skip fn and return this value unchanged.

Return Value

Return ValueDescription
ObjectA shallow copy with fn stored at prop. Other non-primitive values are copied by reference. The original is not mutated. A missing key returns the original object.

Ramda Examples

📚 Getting Started

The official age and pets sample, then the same person with assoc so you see a literal write.

Example 1 — Official Age and Pets

R.add(1) bumps age. R.append('turtle') grows pets. The original person stays put.

Ramda
const person = { name: 'James', age: 20, pets: ['dog', 'cat'] };

R.modify('age', R.add(1), person);
//=> { name: 'James', age: 21, pets: ['dog', 'cat'] }
R.modify('pets', R.append('turtle'), person);
//=> { name: 'James', age: 20, pets: ['dog', 'cat', 'turtle'] }
person;
//=> { name: 'James', age: 20, pets: ['dog', 'cat'] }
Try It Yourself

How It Works

Each call copies person and runs fn on one field. That is the sample from ramdajs.com.

Example 2 — vs R.assoc

assoc writes a number you already know. modify reads the current age and adds one.

Ramda
const person = { name: 'James', age: 20, pets: ['dog', 'cat'] };

R.assoc('age', 99, person);
//=> { name: 'James', age: 99, pets: ['dog', 'cat'] }
R.modify('age', R.add(1), person);
//=> { name: 'James', age: 21, pets: ['dog', 'cat'] }
person;
//=> { name: 'James', age: 20, pets: ['dog', 'cat'] }
Try It Yourself

How It Works

Both leave person intact. Pick assoc when you already know the next value. Pick modify when the next value is a function of the current one.

📈 Practical Patterns

One field vs evolve, missing keys and array indexes, then a curried helper in a pipe.

Example 3 — vs R.evolve

For a single top-level field, evolve and modify match. evolve is the helper when you need a nested recipe.

Ramda
const person = { name: 'James', age: 20, pets: ['dog', 'cat'] };

R.evolve({ age: R.add(1) }, person);
//=> { name: 'James', age: 21, pets: ['dog', 'cat'] }
R.modify('age', R.add(1), person);
//=> { name: 'James', age: 21, pets: ['dog', 'cat'] }
Try It Yourself

How It Works

evolve takes a recipe object. modify takes the key and the function as separate arguments. For one field they agree. Nested transforms still belong to evolve or modifyPath.

Example 4 — Missing Key, Arrays, Shared Refs

A missing property returns the same object. Array indexes work. Unchanged nested values stay shared.

Ramda
const person = { name: 'James', age: 20, pets: ['dog', 'cat'] };
const missing = R.modify('city', R.toUpper, person);
const older = R.modify('age', R.add(1), person);

missing === person;
//=> true
R.modify(0, R.toUpper, ['ada', 'bo']);
//=> ['ADA', 'bo']
older.pets === person.pets;
//=> true
Try It Yourself

How It Works

The docs say the function is not invoked when the property is missing, and non-primitive properties are copied by reference. Mutating older.pets would also change person.pets.

Example 5 — Curry with pipe

Fix the birthday helper first, then read the new age.

Ramda
const birthday = R.modify('age', R.add(1));
const ageAfter = R.pipe(
  R.modify('age', R.add(1)),
  R.prop('age')
);
const person = { name: 'James', age: 20 };

birthday(person);
//=> { name: 'James', age: 21 }
ageAfter(person);
//=> 21
Try It Yourself

How It Works

R.modify('age', R.add(1)) waits for one object, which is why it drops into R.pipe.

Browser Support

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

Load Ramda 0.28.0 or later via CDN or npm, then call R.modify with a property, 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.modify() 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.modify(prop, fn, object) returns a shallow copy where fn is applied to object[prop]. Other own properties are copied. Non-primitive values are copied by reference. Added in v0.28.0. The original object is not mutated.
person is {name: 'James', age: 20, pets: ['dog', 'cat']}. R.modify('age', R.add(1), person) is {name: 'James', age: 21, pets: ['dog', 'cat']}. R.modify('pets', R.append('turtle'), person) is {name: 'James', age: 20, pets: ['dog', 'cat', 'turtle']}. The original person stays {name: 'James', age: 20, pets: ['dog', 'cat']}.
assoc writes a value you already know. modify reads the current value and runs a function on it. Use assoc for a literal. Use modify when the next value depends on the current one, such as R.add(1) or R.append('turtle').
evolve takes a recipe object and can transform many keys, including nested ones. modify updates exactly one property. For one field, R.evolve({age: R.add(1)}, person) and R.modify('age', R.add(1), person) match. Need a nested path? That is R.modifyPath.
fn is not called, and the original object is returned unchanged (same reference). modify does not create the key.
No. R.modify is a stable Ramda object helper from v0.28.0. It is not deprecated, experimental, or non-standard.

Continue to R.modifyPath()

Learn how to apply a function at a nested path using Ramda R.modifyPath().

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