Lodash _.cloneWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Like _.clone, except it accepts a customizer that may produce the cloned value. If the customizer returns undefined, Lodash handles cloning instead. Part of Lodash Lang since 4.0.0 (stable — not deprecated). Warning: cloning is shallow — nested objects stay shared unless you replace them.

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

Syntax

Call _.cloneWith. Start with this line.

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

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

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

Parameters

ParameterDescription
valueRequired. Value to clone
customizerOptional. Hook invoked as (value [, index|key, object, stack])

Return Value

Return ValueDescription
*Cloned value (customized where you returned a value)

Lodash Examples

Example 1 — Shallow Clone: Nested Refs Stay Shared

Top-level object is new; nested objects keep the same reference.

Lodash
var source = { id: 1, meta: { level: 2 } };
var copy = _.cloneWith(source);

console.log(copy !== source);           // true
console.log(copy.meta === source.meta); // true
Try It Yourself

How It Works

  1. Top level. Lodash builds a new object for copy.
  2. Own props. id and meta are copied by assignment.
  3. Nested share. meta still points at the same object.
  4. Tip. Mutating copy.meta also changes source.meta.

Example 2 — Customizer Replaces Numbers

Return a value from the customizer to override that clone.

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

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

console.log(copy);
// → { a: 10, b: 20 }
Try It Yourself

How It Works

  1. Visit. Customizer runs for the root and each own property value.
  2. Match. Numbers return value * 10.
  3. Root object. Returning nothing for the object keeps default shallow clone of the container.
  4. Result. New object with transformed leaves.

Example 3 — Replace a Nested Object Yourself

Return a new nested object when you need independence at one key.

Lodash
var source = { id: 1, meta: { level: 2 } };

var copy = _.cloneWith(source, function (value, key) {
  if (key === "meta") {
    return _.clone(value); // new nested object
  }
});

console.log(copy.meta === source.meta); // false
copy.meta.level = 9;
console.log(source.meta.level);         // 2
Try It Yourself

How It Works

  1. Key check. Customizer sees key === "meta".
  2. Replace. Return _.clone(value) so nested data is not shared.
  3. Other props. id falls through to default shallow rules.
  4. Safe mutate. Changing copy.meta leaves the source at 2.

Example 4 — Customize Date Values

Turn a Date into an ISO string while shallow-cloning the rest.

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

var copy = _.cloneWith(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 property.
  3. Other fields. label uses the default shallow path.
  4. Pattern. Same idea as the official DOM cloneNode(false) customizer.

Example 5 — cloneWith vs cloneDeepWith

Shallow keeps nested refs; deep creates new nested objects.

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. Same hook style. Both accept a customizer; both return undefined here.
  2. Shallow. cloneWith copies only the top level → nest is shared.
  3. Deep. cloneDeepWith recurses → nest is a new object.
  4. Pick. Need nested independence? Prefer cloneDeepWith.

Browser & Runtime Support

_.cloneWith 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 object spread / Object.assign, but not this customizer-based shallow clone by name.

✓ Stable · Lodash 4.0.0+

_.cloneWith()

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

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

Frequently Asked Questions

_.cloneWith(value, [customizer]) is like _.clone, but accepts a customizer. The customizer can produce the cloned value; if it returns undefined, Lodash handles cloning with its default shallow rules.
It is shallow. Nested objects and arrays remain shared with the source unless your customizer returns a replacement for them.
Lodash uses its default shallow cloning behavior for that value. Return any other value (including null or 0) to use it as the clone.
clone uses fixed shallow rules only. cloneWith adds a customizer hook so you can override how certain values are copied.
Use cloneDeepWith when you need recursion plus customization across the whole nested tree.
Use import cloneWith from "lodash/cloneWith" or require("lodash/cloneWith") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.cloneWith.

Next: _.conformsTo()

Check whether an object conforms to a source of property predicates.

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