Ramda.js R.assocPath() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.assocPath() answers a nested question: give me a copy of this object with a value at this path. You pass an array of keys, then the value, then the object. You get a new object. The original is never mutated. Nodes along the path are cloned. Sibling keys stay. Missing or non-object nodes are created so the path can exist.

The official sample is the whole idea. R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}) is {a: {b: {c: 42}}}. Native JavaScript’s cousin is nested spread: { ...obj, a: { ...obj.a, b: { ...obj.a.b, c: 42 } } }. That is data-first and painful to reuse. assocPath is data-last, so R.assocPath(['user', 'name'], 'Lin') is already a function of one object — ready for R.map or R.pipe. One top-level key? That is R.assoc. Need to drop a nested key? That is R.dissocPath. Lodash _.set writes in place and accepts dotted strings; Ramda clones and wants a path array.

Syntax

Call R.assocPath. Start with this line.

Ramda
R.assocPath(path, val, obj)
CallWhat it does
R.assocPath(path, val, obj)Primary signature.
R.assocPath(...)Call the object helper.

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

Library declarations
Ramda
R.assocPath(path, val, obj)

Parameters

ParameterDescription
pathRequired. Array. Keys to walk. Strings (and symbols) are object keys. Integers are array indexes. An empty path returns val.
valRequired. Any. The value stored at the tail of path on the clone.
objRequired. Object or Array. The source. It is never mutated. A new object (or array) is returned at each level of the path.

Return Value

Return ValueDescription
Object or ArrayA clone equal to the original except along path, where val sits at the tail. Enumerable prototype properties are flattened onto new objects. Nodes off the path stay shared by reference.

Ramda Examples

📚 Getting Started

The official Ramda sample, then the same path on an object that does not already hold nested objects.

Example 1 — Official Nested Path

The tail of the path is replaced on a clone. Sibling structure under a and b is kept.

Ramda
R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}});
//=> {a: {b: {c: 42}}}
Try It Yourself

How It Works

Ramda clones a, then clones b, then writes c: 42. The input tree is not reused as the return value. That is the docs sample from ramdajs.com.

Example 2 — Missing or Non-Object Nodes

If a key along the path is missing or is not an object, Ramda replaces it so the rest of the path can exist. The original value stays 5.

Ramda
const orig = {a: 5};

R.assocPath(['a', 'b', 'c'], 42, orig);
//=> {a: {b: {c: 42}}}

orig;
//=> {a: 5}
Try It Yourself

How It Works

orig.a is the number 5, so it cannot hold b. Ramda builds a fresh object tree {b: {c: 42}} and stores it at a on a clone. orig.a is still 5. That is the opposite of orig.a.b.c = 42, which would throw, and of Lodash _.set, which mutates.

📈 Practical Patterns

Integer indexes, sibling keys that assoc would drop, then a curried helper and integer vs string keys.

Example 3 — Array Index in the Path

An integer path segment means “this node is an array.” Index 1 on an empty list leaves a hole at 0.

Ramda
const result = R.assocPath(['a', 1, 'c'], 42, {a: []});

result;
//=> {a: [undefined, {c: 42}]}

result.a[0] === undefined;
//=> true
Try It Yourself

How It Works

Path ['a', 1, 'c'] means: key a, then array index 1, then key c. The empty array is cloned long enough to hold index 1. Slot 0 is undefined. JSON.stringify prints that hole as null — the Try-it lab shows both the JSON and the === undefined check so the hole is obvious.

Example 4 — vs assoc, Siblings Kept

assoc replaces the whole nested object. assocPath clones only along the path, so sibling keys such as city stay. The original tree is not mutated, and the nested user object is a new reference.

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

R.assoc('user', { name: 'Lin' }, orig);
//=> { user: { name: 'Lin' } }

R.assocPath(['user', 'name'], 'Lin', orig);
//=> { user: { name: 'Lin', city: 'Paris' } }

orig;
//=> { user: { name: 'Ada', city: 'Paris' } }

R.assocPath(['user', 'name'], 'Lin', orig).user === orig.user;
//=> false
Try It Yourself

How It Works

assoc swapped the entire user value, so city disappeared unless you rebuilt it by hand. assocPath cloned user, wrote name, and copied city across. orig.user still holds Ada. That is why nested updates belong here, not on R.assoc.

Example 5 — Curry, Map, Integer vs String

Fill the path and value to get a function of one object. Map it over rows. An integer creates an array; a string digit creates an object key.

Ramda
const setName = R.assocPath(['user', 'name'], 'Lin');

setName({ user: { name: 'Ada' } });
//=> { user: { name: 'Lin' } }

R.map(setName, [{ user: { name: 'Ada' } }, { user: { name: 'Bo' } }]);
//=> [{ user: { name: 'Lin' } }, { user: { name: 'Lin' } }]

R.assocPath(['a', 1], 42, {});
//=> { a: [undefined, 42] }

R.assocPath(['a', '1'], 42, {});
//=> { a: { '1': 42 } }
Try It Yourself

How It Works

Because assocPath is arity 3, filling path and val leaves a function of one object, ready for R.map and R.pipe. The last two calls are the array-vs-object rule: peek at the next path segment; an integer builds [], a string builds {}. JSON.stringify prints the array hole as null.

Browser Support

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

✓ Ramda 0.8.0+ · Any JS runtime

Ramda.js R.assocPath()

Load Ramda 0.8.0 or later via CDN or npm, then call R.assocPath with a path array, a value, and an object. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.8.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]. Negative array indexes in the path need Ramda 0.31+.

Frequently Asked Questions

R.assocPath(path, val, obj) returns a shallow clone of obj with val placed at the tail of path. Missing or non-object nodes along the path are created. Added in v0.8.0. The original object is not mutated.
R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}) is {a: {b: {c: 42}}}. Only c changes. Sibling keys off that path stay on the clone.
No. It returns a new object (or array) at every level of the path. Nodes not on the path are copied by reference, so the clone is shallow except along that path.
assoc sets one top-level key. assocPath walks an array of keys such as ['user', 'name']. dissocPath is the inverse: it removes the tail of a path on a clone.
Integers are array indexes. Strings are object keys. R.assocPath(['a', 1], 42, {}) is {a: [undefined, 42]}. R.assocPath(['a', '1'], 42, {}) is {a: {'1': 42}}.
No. R.assocPath is a stable Ramda object helper from v0.8.0. It is not deprecated, experimental, or non-standard.

Continue to R.clone()

Learn how to copy a whole nested tree using Ramda R.clone().

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