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 toMerged
toMerged<
T extends Record<PropertyKey, any>,
S extends Record<PropertyKey, any>,
>
(
target: T,
source: S,
): T & S

Merges the properties of the source object into a deep clone of the target object. Unlike merge, This function does not modify the original target object.

This function performs a deep merge, meaning nested objects and arrays are merged recursively.

  • If a property in the source object is an array or object and the corresponding property in the target object is also an array or object, they will be merged.
  • If a property in the source object is undefined, it will not overwrite a defined property in the target object.

Note that this function does not mutate the target object.

Examples

const target = { a: 1, b: { x: 1, y: 2 } }; const source = { b: { y: 3, z: 4 }, c: 5 };

const result = toMerged(target, source); console.log(result); // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }

const target = { a: [1, 2], b: { x: 1 } }; const source = { a: [3], b: { y: 2 } };

const result = toMerged(target, source); console.log(result); // Output: { a: [3, 2], b: { x: 1, y: 2 } }

const target = { a: null }; const source = { a: [1, 2, 3] };

const result = toMerged(target, source); console.log(result); // Output: { a: [1, 2, 3] }

Type Parameters

T extends Record<PropertyKey, any>
  • Type of the target object.
S extends Record<PropertyKey, any>
  • Type of the source object.

Parameters

target: T
  • The target object to be cloned and merged into. This object is not modified directly.
source: S
  • The source object whose properties will be merged into the cloned target object.

Return Type

T & S

A new object with properties from the source object merged into a deep clone of the target object.

Add Package

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

Import symbol

import { toMerged } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

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

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { toMerged } from "@es-toolkit/es-toolkit/compat";

Add Package

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

Import symbol

import { toMerged } from "@es-toolkit/es-toolkit/compat";

Add Package

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

Import symbol

import { toMerged } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { toMerged } from "@es-toolkit/es-toolkit/compat";