Ramda.js R.mergeWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.mergeWith() answers a one-level merge question: how do I mix two records so unique keys stay, while I still choose what happens when a field appears twice? You pass a clash function, then the left object, then the right object. You get a new object. Neither source is mutated.

The official sample is the whole idea. Left has a: true and values: [10, 20]. Right has b: true and values: [15, 35]. Unique keys stay. The clash function is R.concat, so the arrays join instead of replacing. Need the second array to win with no concat? That is R.mergeRight. Need the clash to see the key name? That is R.mergeWithKey. Need nested objects to mix first? That is R.mergeDeepWith.

Syntax

Call R.mergeWith. Start with this line.

Ramda
R.mergeWith(fn, l, r)
CallWhat it does
R.mergeWith(fn, l, r)Primary signature.
R.mergeWith(...)Call the object helper.

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

Library declarations
Ramda
R.mergeWith(fn, l, r)

Parameters

ParameterDescription
fnRequired. Function. Called as fn(leftValue, rightValue) when a key exists on both sides.
lRequired. Object. The first record. Own keys that do not exist on the right are copied.
rRequired. Object. The second record. Own keys that do not exist on the left are copied. Nested objects are not walked.

Return Value

Return ValueDescription
ObjectA new object with own properties from both sides. Clash values come from fn. The source objects are not mutated.

Ramda Examples

📚 Getting Started

The official concat, then the same inputs with mergeRight so the array is replaced.

Example 1 — Official concat

Unique keys a and b are kept. Matching values arrays join.

Ramda
R.mergeWith(R.concat,
  { a: true, values: [10, 20] },
  { b: true, values: [15, 35] }
);
//=> { a: true, b: true, values: [10, 20, 15, 35] }
Try It Yourself

How It Works

Left keys are copied first. values exists on both sides, so R.concat runs. Then right-only b is added. That is the docs example from ramdajs.com.

Example 2 — vs R.mergeRight

Same two records. Concat joins the list. Right-wins replaces it.

Ramda
const left = { a: true, values: [10, 20] };
const right = { b: true, values: [15, 35] };

R.mergeWith(R.concat, left, right);
//=> { a: true, b: true, values: [10, 20, 15, 35] }
R.mergeRight(left, right);
//=> { a: true, values: [15, 35], b: true }
Try It Yourself

How It Works

Both helpers keep a and b. Only the clash rule for values changes.

📈 Practical Patterns

Shallow vs deep with R.add, add scores with extra keys, then a curried concat in a pipe.

Example 3 — vs R.mergeDeepWith

Shallow merge calls R.add on the nested objects themselves, which is NaN. Deep merge adds the inner numbers.

Ramda
const left = { a: 1, nest: { x: 2 } };
const right = { a: 3, nest: { x: 4 } };

R.mergeWith(R.add, left, right).nest;
//=> NaN
R.mergeDeepWith(R.add, left, right);
//=> { a: 4, nest: { x: 6 } }
Try It Yourself

How It Works

R.mergeWith never walks inside nest. R.add on two objects is not a number. Deep merge waits until x is two numbers.

Example 4 — Add scores and extra keys

R.add joins matching numbers. Left-only name is still copied. Sources stay unchanged.

Ramda
const left = { score: 10, name: 'Ada' };
const right = { score: 7, extra: true };

R.mergeWith(R.add, left, right);
//=> { score: 17, name: 'Ada', extra: true }
left;
//=> { score: 10, name: 'Ada' }
right;
//=> { score: 7, extra: true }
Try It Yourself

How It Works

Only score clashes, so only that field runs R.add. The original objects are never assigned to.

Example 5 — Curry with pipe

Fix a concat helper first, then read tags. Both helpers are data-last.

Ramda
const concatVals = R.mergeWith(R.concat);
const tagList = R.pipe(
  R.mergeWith(R.concat, { tags: ['js'] }),
  R.prop('tags')
);
const extra = { tags: ['fp'], title: 'Notes' };

concatVals({ tags: ['js'], a: ['x'] }, { tags: ['fp'], a: ['y'] });
//=> { tags: ['js', 'fp'], a: ['x', 'y'] }
tagList(extra);
//=> ['js', 'fp']
Try It Yourself

How It Works

R.mergeWith(R.concat) waits for two objects. R.mergeWith(R.concat, seed) waits for one object, which is why it drops into R.pipe.

Browser Support

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

Load Ramda 0.19.0 or later via CDN or npm, then call R.mergeWith with a clash function and two objects. 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.mergeWith() 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.mergeWith(fn, l, r) returns a new object with own properties from both records. On a clash, fn(leftValue, rightValue) becomes the stored value. Unique keys are copied as-is. Added in v0.19.0. Neither source object is mutated. The merge is shallow.
R.mergeWith(R.concat, { a: true, values: [10, 20] }, { b: true, values: [15, 35] }) is { a: true, b: true, values: [10, 20, 15, 35] }. Unique keys a and b are kept. The values arrays are concatenated because the clash function is R.concat.
Both are shallow. mergeRight always keeps the second value on a clash. mergeWith lets you pick the clash, so R.concat can join arrays instead of replacing them.
mergeWith is shallow: a nested object clash calls fn on the two objects themselves. mergeDeepWith walks inside those objects first, and only then calls fn on leaves such as numbers or arrays.
No. fn is (leftValue, rightValue) => value. Need the key too? That is R.mergeWithKey.
No. R.mergeWith is a stable Ramda object helper from v0.19.0. It is not deprecated, experimental, or non-standard.

Continue to R.mergeWithKey()

Learn how to shallow-merge two objects with a clash function that also receives the key using Ramda R.mergeWithKey().

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