Ramda.js R.assoc() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.assoc() answers a simple question: give me a copy of this object with one property set. You pass the key, then the value, then the object. You get a new object. The original is never mutated. If the key is new, it is added. If it already exists, the clone holds the new value.

The official sample is the whole idea. R.assoc('c', 3, {a: 1, b: 2}) is {a: 1, b: 2, c: 3}. Native JavaScript’s cousin is { ...obj, c: 3 } or Object.assign({}, obj, { c: 3 }). Those are data-first. assoc is data-last, so R.assoc('c', 3) is already a function of one object — ready for R.map or R.pipe. Need a nested key? That is R.assocPath. Need to drop a key? That is R.dissoc. Lodash _.set writes in place; Ramda clones.

Syntax

Call R.assoc. Start with this line.

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

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

Library declarations
Ramda
R.assoc(prop, val, obj)

Parameters

ParameterDescription
propRequired. String or Int. The property name to set. An integer key on an array replaces that index on a new array.
valRequired. Any. The value stored at prop on the clone.
objRequired. Object or Array. The source. It is never mutated. A new object (or array) is returned.

Return Value

Return ValueDescription
Object or ArrayA shallow clone with prop set to val. Enumerable own properties (and enumerable prototype properties) are copied onto the new object. Nested objects stay shared by reference.

Ramda Examples

📚 Getting Started

The official Ramda sample, then the same object with an overwritten key so add vs replace is obvious.

Example 1 — Official Object

A new key is added on a clone. The leftover is every original property plus c: 3.

Ramda
R.assoc('c', 3, {a: 1, b: 2});
//=> {a: 1, b: 2, c: 3}
Try It Yourself

How It Works

Ramda copies a and b onto a new object, then writes c: 3. The input literal is not reused as the return value.

Example 2 — Overwrite and Copy

The same helper replaces an existing key. The original object still holds a: 1.

Ramda
const orig = {a: 1, b: 2};

R.assoc('a', 9, orig);
//=> {a: 9, b: 2}

orig;
//=> {a: 1, b: 2}
Try It Yourself

How It Works

The clone gets a: 9. orig.a stays 1 because assoc never assigns onto the source. That is the opposite of orig.a = 9 and of Lodash _.set.

📈 Practical Patterns

Native spread and dissoc, then a shallow nested copy versus assocPath, then a curried helper and an array index.

Example 3 — vs Spread and dissoc

Spread and assoc both add c: 3. dissoc is the inverse: it removes a key on a clone.

Ramda
const obj = {a: 1, b: 2};

R.assoc('c', 3, obj);
//=> {a: 1, b: 2, c: 3}

Object.assign({}, obj, {c: 3});
//=> {a: 1, b: 2, c: 3}

R.dissoc('a', {a: 1, b: 2, c: 3});
//=> {b: 2, c: 3}
Try It Yourself

How It Works

Spread and Object.assign are data-first. assoc is data-last, so the key and value can be filled first. dissoc is the docs’ see-also for the opposite edit: drop a property instead of setting one.

Example 4 — Shallow Copy vs assocPath

Nested objects are shared by reference after assoc. Nested updates belong to assocPath, which clones along the path.

Ramda
const orig = { user: { name: 'Ada' } };
const withRole = R.assoc('role', 'dev', orig);

withRole;
//=> { user: { name: 'Ada' }, role: 'dev' }

withRole.user === orig.user;
//=> true

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

orig;
//=> { user: { name: 'Ada' } }
Try It Yourself

How It Works

assoc copied the user pointer, then added role. Changing withRole.user.name would also change orig.user.name. assocPath clones the user object so name can change on a new tree. That is the next Object helper.

Example 5 — Curry and Arrays

Fill the key and value to get a function of one object. Map it over rows. An integer key on an array replaces that index on a new array.

Ramda
const setC = R.assoc('c', 3);

setC({a: 1});
//=> {a: 1, c: 3}

R.map(setC, [{id: 1}, {id: 2}]);
//=> [{id: 1, c: 3}, {id: 2, c: 3}]

R.assoc(1, 'x', ['a', 'b', 'c']);
//=> ['a', 'x', 'c']
Try It Yourself

How It Works

Because assoc is arity 3, filling prop and val leaves a function of one object, ready for R.map and R.pipe. Integer keys on arrays go through the array branch: a new array, same values, one index replaced. For a function of the old value, List R.update and R.adjust are the named cousins.

Browser Support

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

Load Ramda 0.8.0 or later via CDN or npm, then call R.assoc with a key, 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.assoc() 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].

Frequently Asked Questions

R.assoc(prop, val, obj) returns a shallow clone of obj with prop set to val. If the key is new, it is added. If it already exists, it is overwritten. Added in v0.8.0. The original object is not mutated.
R.assoc('c', 3, {a: 1, b: 2}) is {a: 1, b: 2, c: 3}. The new object has every original property plus c: 3.
No. It always returns a new object (or a new array when prop is an integer and obj is an array). Nested objects and arrays are copied by reference, so the clone is shallow.
assoc sets one top-level key. assocPath sets a nested path such as ['user', 'name']. dissoc removes a key on a clone. The docs also point to R.pick for keeping only some keys.
{ ...obj, c: 3 } is the native cousin. assoc is curried and data-last, so R.assoc('c', 3) is already a function of one object. Spread is data-first and not reusable in R.map without a wrapper.
No. R.assoc is a stable Ramda object helper from v0.8.0. It is not deprecated, experimental, or non-standard.

Continue to R.assocPath()

Learn how to set a nested path on a cloned object using Ramda R.assocPath().

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