Merge Deep

Merges and deep copies the values of all enumerable own properties of the target object from the source object to a new object.

CategoryObjects
AuthorPol Gubau
Since1.0.0
Last Updated2025-03-15
TypeSync
CompatibilityUniversal

Example

  • Input

    mergeDeep({ a: { x: 1 } }, { a: { y: 2 }, b: 3 })

    Output:

    { a: { x: 1, y: 2 }, b: 3 }

Return

A new object that is the result of merging and deeply copying properties from the source object into the target object.

Type: T & S

Props

NameTypeDefaultRequiredDescription
targetT extends objectnullYesThe target object from which to copy properties.
sourceS extends objectnullYesThe source object from which to merge properties.

Notes

This function creates a new object by merging and deep copying properties from the source object into the target object. It performs a deep merge, meaning nested objects are merged recursively. If a nested object is found in both the target and source, it will be recursively merged. If the source object is empty, a deep copy of the target is returned.

Tags

  • merge
  • deep merge
  • object
  • recursive
  • copy
  • modification

Related