Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function cloneDeepWith
cloneDeepWith<T>(
obj: T,
cloneValue: (
value: any,
key: PropertyKey | undefined,
obj: T,
stack: Map<any, any>,
) => any
,
): T

Deeply clones the given object.

You can customize the deep cloning process using the cloneValue function. The function takes the current value value, the property name key, and the entire object obj as arguments. If the function returns a value, that value is used; if it returns undefined, the default cloning method is used.

Examples

Example 1

// Clone a primitive value const num = 29; const clonedNum = cloneDeepWith(num); console.log(clonedNum); // 29 console.log(clonedNum === num); // true

Example 2

// Clone an object with a customizer const obj = { a: 1, b: 2 }; const clonedObj = cloneDeepWith(obj, (value) => { if (typeof value === 'number') { return value * 2; // Double the number } }); console.log(clonedObj); // { a: 2, b: 4 } console.log(clonedObj === obj); // false

Example 3

// Clone an array with a customizer const arr = [1, 2, 3]; const clonedArr = cloneDeepWith(arr, (value) => { return value + 1; // Increment each value }); console.log(clonedArr); // [2, 3, 4] console.log(clonedArr === arr); // false

Type Parameters

  • The type of the object.

Parameters

obj: T
  • The object to clone.
optional
cloneValue: (
value: any,
key: PropertyKey | undefined,
obj: T,
stack: Map<any, any>,
) => any
  • A function to customize the cloning process.

Return Type

  • A deep clone of the given object.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { cloneDeepWith } from "@es-toolkit/es-toolkit";

---- OR ----

Import directly with a jsr specifier

import { cloneDeepWith } from "jsr:@es-toolkit/es-toolkit";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { cloneDeepWith } from "@es-toolkit/es-toolkit";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { cloneDeepWith } from "@es-toolkit/es-toolkit";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { cloneDeepWith } from "@es-toolkit/es-toolkit";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { cloneDeepWith } from "@es-toolkit/es-toolkit";