Lodash _.assignWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Like _.assign, but accepts a customizer to produce each assigned value. If the customizer returns undefined, assignment is handled normally. Own enumerable string keys only. Since Lodash 4.0.0.

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.assignWith. Start with this line.

Lodash
_.assignWith(object, ...sources, customizer)
CallWhat it does
_.assignWith(object, ...sources, customizer)Primary signature.
_.assignWith(object)Work with an object’s keys, values, or paths.

Prefer import assignWith from "lodash/assignWith" when you want a smaller bundle.

Library declarations
Lodash
_.assignWith(object, ...sources, customizer)

Parameters

ParameterDescription
objectRequired. Destination (mutated and returned)
...sourcesOptional. Own enumerable string keys only
customizerRequired. Last argument — produces assigned values

Return Value

Return ValueDescription
*The value returned by _.assignWith().

Lodash Examples

Example 1 — Official: Defaults-Style Customizer

Keep existing values; only fill keys that are still undefined.

Lodash
function customizer(objValue, srcValue) {
  return _.isUndefined(objValue) ? srcValue : objValue;
}

var defaults = _.partialRight(_.assignWith, customizer);

defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
// → { 'a': 1, 'b': 2 }
Try It Yourself

How It Works

  1. Customizer. Prefer the destination value when it is defined.
  2. partialRight. Binds the customizer as the last argument.
  3. Sources. { b: 2 } fills a missing key; { a: 3 } cannot overwrite a: 1.
  4. Related. Similar idea to _.defaults.

Example 2 — Direct assignWith Call

Same customizer without partialRight.

Lodash
function customizer(objValue, srcValue) {
  return _.isUndefined(objValue) ? srcValue : objValue;
}

_.assignWith({ a: 1 }, { b: 2 }, { a: 3 }, customizer);
// → { a: 1, b: 2 }
Try It Yourself

How It Works

  1. Last arg. The customizer must be the final argument.
  2. Sources in the middle. Any number of sources can appear before it.
  3. Same rule. Keep defined destination values; take sources for missing keys.
  4. Mutates. The first object is updated in place.

Example 3 — Without Customizer (assign)

Plain assign overwrites; the customizer can prevent that.

Lodash
_.assign({ a: 1 }, { a: 3, b: 2 });
// → { a: 3, b: 2 }

_.assignWith({ a: 1 }, { a: 3, b: 2 }, function (objValue, srcValue) {
  return _.isUndefined(objValue) ? srcValue : objValue;
});
// → { a: 1, b: 2 }
Try It Yourself

How It Works

  1. assign. Later source wins → a becomes 3.
  2. assignWith. Customizer keeps a: 1.
  3. New keys. b is still filled because it was undefined.
  4. Use. Reach for With when overwrite rules are not “last wins”.

Example 4 — Sum Matching Numbers

Customizer can combine values instead of replacing them.

Lodash
function sumNumbers(objValue, srcValue) {
  if (_.isNumber(objValue) && _.isNumber(srcValue)) {
    return objValue + srcValue;
  }
}

_.assignWith({ a: 1, b: "x" }, { a: 2, b: "y", c: 3 }, sumNumbers);
// → { a: 3, b: "y", c: 3 }
Try It Yourself

How It Works

  1. Both numbers on key a → return sum 3.
  2. Strings on b → customizer returns undefined → default "y".
  3. New key c → objValue undefined → default 3.
  4. Pattern. Override only the pairs you care about.

Example 5 — Own Keys Only (vs assignInWith)

Prototype keys are skipped — that is the main difference from assignInWith.

Lodash
function Foo() {
  this.a = 1;
}
Foo.prototype.b = 2;

function customizer(objValue, srcValue) {
  return _.isUndefined(objValue) ? srcValue : objValue;
}

_.assignWith({ a: 9 }, new Foo(), customizer);
// → { a: 9 }   // own keys only — no b

_.assignInWith({ a: 9 }, new Foo(), customizer);
// → { a: 9, b: 2 }
Try It Yourself

How It Works

  1. Foo instance owns a; inherits b.
  2. assignWith. Skips inherited b entirely.
  3. assignInWith. Includes b, then customizes.
  4. Pick assignWith when you do not want prototype surprises.

Browser & Runtime Support

_.assignWith is a Lodash Object helper (since 4.0.0). It runs anywhere Lodash 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.assignWith()

Available in [email protected] full builds and the lodash/assignWith modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.assignWith Lodash 4.0.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import assignWith from "lodash/assignWith". Use assignInWith when inherited keys matter, or mergeWith for deep custom merges.

Frequently Asked Questions

_.assignWith(object, ...sources, customizer) is like _.assign, but each assigned value can be produced by your customizer. If the customizer returns undefined, Lodash assigns the source value as usual.
Five arguments: (objValue, srcValue, key, object, source).
Same customizer idea. _.assignWith walks own enumerable string keys only; _.assignInWith also walks inherited keys.
Yes. Use _.assignWith({}, ...sources, customizer) when you want a new object.
No. Assignment is shallow. For nested merges with a customizer, use _.mergeWith.
Use import assignWith from "lodash/assignWith" or require("lodash/assignWith"). Full build: import _ from "lodash" then _.assignWith.

Next: _.at()

Create an array of values corresponding to paths of an object.

_.at() 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.

11 people found this page helpful