Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
deepmerge-ts
Deeply merge 2 or more objects respecting type information.
ts-deepmerge
A TypeScript deep merge function.
@fastify/deepmerge
Merges the enumerable properties of two or more objects deeply.
@corex/deepmerge
A zero dependency object merger with typescript support and built in common merge utilities.
A library for deep (recursive) merging of Javascript objects
npm install deepmerge
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (95.85%)
TypeScript (4.15%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2,799 Stars
331 Commits
219 Forks
16 Watchers
4 Branches
34 Contributors
Updated on Jul 08, 2025
Latest Version
4.3.1
Package Id
deepmerge@4.3.1
Unpacked Size
30.43 kB
Size
8.25 kB
File Count
11
NPM Version
8.6.0
Node Version
16.8.0
Published on
Mar 16, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Merges the enumerable properties of two or more objects deeply.
UMD bundle is 723B minified+gzipped
1const x = { 2 foo: { bar: 3 }, 3 array: [{ 4 does: 'work', 5 too: [ 1, 2, 3 ] 6 }] 7} 8 9const y = { 10 foo: { baz: 4 }, 11 quux: 5, 12 array: [{ 13 does: 'work', 14 too: [ 4, 5, 6 ] 15 }, { 16 really: 'yes' 17 }] 18} 19 20const output = { 21 foo: { 22 bar: 3, 23 baz: 4 24 }, 25 array: [{ 26 does: 'work', 27 too: [ 1, 2, 3 ] 28 }, { 29 does: 'work', 30 too: [ 4, 5, 6 ] 31 }, { 32 really: 'yes' 33 }], 34 quux: 5 35} 36 37merge(x, y) // => output
With npm do:
1npm install deepmerge
deepmerge can be used directly in the browser without the use of package managers/bundlers as well: UMD version from unpkg.com.
deepmerge exposes a CommonJS entry point:
const merge = require('deepmerge')
The ESM entry point was dropped due to a Webpack bug.
merge(x, y, [options])
Merge two objects x
and y
deeply, returning a new merged object with the
elements from both x
and y
.
If an element at the same key is present for both x
and y
, the value from
y
will appear in the result.
Merging creates a new object, so that neither x
or y
is modified.
Note: By default, arrays are merged by concatenating them.
merge.all(arrayOfObjects, [options])
Merges any number of objects into a single result object.
1const foobar = { foo: { bar: 3 } } 2const foobaz = { foo: { baz: 4 } } 3const bar = { bar: 'yay!' } 4 5merge.all([ foobar, foobaz, bar ]) // => { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
arrayMerge
There are multiple ways to merge two arrays, below are a few examples but you can also create your own custom function.
Your arrayMerge
function will be called with three arguments: a target
array, the source
array, and an options
object with these properties:
isMergeableObject(value)
cloneUnlessOtherwiseSpecified(value, options)
arrayMerge
example: overwrite target arrayOverwrites the existing array values completely rather than concatenating them:
1const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray 2 3merge( 4 [1, 2, 3], 5 [3, 2, 1], 6 { arrayMerge: overwriteMerge } 7) // => [3, 2, 1]
arrayMerge
example: combine arraysCombines objects at the same index in the two arrays.
This was the default array merging algorithm pre-version-2.0.0.
1const combineMerge = (target, source, options) => { 2 const destination = target.slice() 3 4 source.forEach((item, index) => { 5 if (typeof destination[index] === 'undefined') { 6 destination[index] = options.cloneUnlessOtherwiseSpecified(item, options) 7 } else if (options.isMergeableObject(item)) { 8 destination[index] = merge(target[index], item, options) 9 } else if (target.indexOf(item) === -1) { 10 destination.push(item) 11 } 12 }) 13 return destination 14} 15 16merge( 17 [{ a: true }], 18 [{ b: true }, 'ah yup'], 19 { arrayMerge: combineMerge } 20) // => [{ a: true, b: true }, 'ah yup']
isMergeableObject
By default, deepmerge clones every property from almost every kind of object.
You may not want this, if your objects are of special types, and you want to copy the whole object instead of just copying its properties.
You can accomplish this by passing in a function for the isMergeableObject
option.
If you only want to clone properties of plain objects, and ignore all "special" kinds of instantiated objects, you probably want to drop in is-plain-object
.
1const { isPlainObject } = require('is-plain-object') 2 3function SuperSpecial() { 4 this.special = 'oh yeah man totally' 5} 6 7const instantiatedSpecialObject = new SuperSpecial() 8 9const target = { 10 someProperty: { 11 cool: 'oh for sure' 12 } 13} 14 15const source = { 16 someProperty: instantiatedSpecialObject 17} 18 19const defaultOutput = merge(target, source) 20 21defaultOutput.someProperty.cool // => 'oh for sure' 22defaultOutput.someProperty.special // => 'oh yeah man totally' 23defaultOutput.someProperty instanceof SuperSpecial // => false 24 25const customMergeOutput = merge(target, source, { 26 isMergeableObject: isPlainObject 27}) 28 29customMergeOutput.someProperty.cool // => undefined 30customMergeOutput.someProperty.special // => 'oh yeah man totally' 31customMergeOutput.someProperty instanceof SuperSpecial // => true
customMerge
Specifies a function which can be used to override the default merge behavior for a property, based on the property name.
The customMerge
function will be passed the key for each property, and should return the function which should be used to merge the values for that property.
It may also return undefined, in which case the default merge behaviour will be used.
1const alex = { 2 name: { 3 first: 'Alex', 4 last: 'Alexson' 5 }, 6 pets: ['Cat', 'Parrot'] 7} 8 9const tony = { 10 name: { 11 first: 'Tony', 12 last: 'Tonison' 13 }, 14 pets: ['Dog'] 15} 16 17const mergeNames = (nameA, nameB) => `${nameA.first} and ${nameB.first}` 18 19const options = { 20 customMerge: (key) => { 21 if (key === 'name') { 22 return mergeNames 23 } 24 } 25} 26 27const result = merge(alex, tony, options) 28 29result.name // => 'Alex and Tony' 30result.pets // => ['Cat', 'Parrot', 'Dog']
clone
Deprecated.
Defaults to true
.
If clone
is false
then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
With npm do:
1npm test
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
9 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More