Lodash _.cloneDeepWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Like _.cloneWith, except it recursively clones value. Your customizer can replace any visited node; return undefined to keep Lodash’s default deep-clone path. Part of Lodash Lang since 4.0.0 (stable — not deprecated). Note: the customizer runs for many nodes — keep it cheap.

Pass the value you want to inspect or convert. Read the return type carefully before you use the result.

Syntax

Call _.cloneDeepWith. Start with this line.

Lodash
_.cloneDeepWith(value, [customizer])
CallWhat it does
_.cloneDeepWith(value, [customizer])Primary signature.
_.cloneDeepWith(value)Check, compare, clone, or convert a value.

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

Library declarations
Lodash
_.cloneDeepWith(value, [customizer])

Parameters

ParameterDescription
valueRequired. Value to recursively clone
customizerOptional. Hook that may replace any visited node

Return Value

Return ValueDescription
*Deep-cloned value (customized where you returned a value)

Lodash Examples

Example 1 — Transform Nested Numbers

Return a value from the customizer to replace that node in the clone.

Lodash
var source = { a: 1, nest: { b: 2 } };

var copy = _.cloneDeepWith(source, function (value) {
  if (typeof value === "number") {
    return value * 2;
  }
});

console.log(copy);
// → { a: 2, nest: { b: 4 } }
Try It Yourself

How It Works

  1. Visit. Lodash walks every nested value.
  2. Match. Numbers hit the customizer → return value * 2.
  3. Skip objects. For source / nest, return nothing → default deep clone of containers.
  4. Result. Nested structure preserved; leaves doubled.

Example 2 — Return undefined for Default Deep Clone

When the customizer does nothing, behavior matches _.cloneDeep.

Lodash
var source = { nest: { n: 1 } };

var copy = _.cloneDeepWith(source, function () {
  // return undefined → default deep clone
});

console.log(copy.nest === source.nest); // false
copy.nest.n = 99;
console.log(source.nest.n);             // 1
Try It Yourself

How It Works

  1. Hook. Customizer returns undefined for every node.
  2. Fallback. Lodash deep-clones objects and arrays normally.
  3. Independence. copy.nest is a new object — not the same reference.
  4. Safe mutate. Changing the clone leaves the source at 1.

Example 3 — Keep the Same Function Reference

Reuse function identity instead of cloning (functions are not usefully deep-cloned).

Lodash
var id = function (x) { return x; };
var source = { scale: 2, fn: id };

var copy = _.cloneDeepWith(source, function (value) {
  if (typeof value === "function") {
    return value; // same reference
  }
});

console.log(copy.fn === source.fn); // true
console.log(copy.scale);            // 2
Try It Yourself

How It Works

  1. Detect. Customizer checks typeof value === "function".
  2. Reuse. Returning value keeps the same function object.
  3. Numbers. scale falls through to default cloning → 2.
  4. Use. Handy when callbacks should stay shared across copies.

Example 4 — Customize Date Values

Turn dates into ISO strings while deep-cloning the rest.

Lodash
var source = {
  label: "ship",
  when: new Date("2026-01-15T00:00:00.000Z")
};

var copy = _.cloneDeepWith(source, function (value) {
  if (value instanceof Date) {
    return value.toISOString();
  }
});

console.log(copy);
// → { label: "ship", when: "2026-01-15T00:00:00.000Z" }
Try It Yourself

How It Works

  1. Match Dates. instanceof Date triggers a custom return.
  2. Serialize. toISOString() becomes the cloned leaf.
  3. Other fields. label uses the default clone path.
  4. Pattern. Same idea as the official DOM cloneNode customizer.

Example 5 — cloneDeepWith vs cloneWith

Shallow customizer leaves nested objects shared; deep does not.

Lodash
var source = { nest: { n: 1 } };

var shallow = _.cloneWith(source, function () {});
var deep = _.cloneDeepWith(source, function () {});

console.log(shallow.nest === source.nest); // true
console.log(deep.nest === source.nest);    // false
Try It Yourself

How It Works

  1. Shallow. cloneWith copies only the top level → nest is shared.
  2. Deep. cloneDeepWith recurses → nest is a new object.
  3. Customizer. Both accept the same hook style; depth is the difference.
  4. Pick. Need nested independence? Prefer cloneDeepWith.

Browser & Runtime Support

_.cloneDeepWith is a Lodash 4.x Lang helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines have structuredClone, but not this customizer-based deep clone by name.

✓ Stable · Lodash 4.0.0+

_.cloneDeepWith()

Available in [email protected] full builds and the lodash/cloneDeepWith 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
_.cloneDeepWith Lodash 4.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import cloneDeepWith from "lodash/cloneDeepWith" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.cloneDeepWith(value, [customizer]) is like _.cloneWith, but it recursively clones value. Your customizer can replace any visited node; return undefined to keep Lodash’s default deep-clone behavior for that node.
If the customizer returns undefined, Lodash applies its normal deep-cloning logic for that value. Return any other value to use it as the cloned result for that node.
No. It builds a new structure from cloning rules and your customizer. The original value is left unchanged.
cloneDeep always uses Lodash’s built-in deep rules. cloneDeepWith adds a customizer hook so you can override cloning for Dates, DOM nodes, functions, or other special values.
cloneWith is shallow — nested objects stay shared unless you replace them. cloneDeepWith recurses through the whole tree with the same customizer pattern.
Use import cloneDeepWith from "lodash/cloneDeepWith" or require("lodash/cloneDeepWith") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.cloneDeepWith.

Next: _.cloneWith()

Shallow-clone a value with the same customizer hook pattern.

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

10 people found this page helpful