Deep Merge
JS Functions
Medium
25 min
objects
recursion
lodash
security
Implement deepMerge(...objects). It merges any number of plain objects left to right into a brand-new object. Nested plain objects are merged recursively instead of being replaced wholesale.
This is how theme systems (createTheme(base, overrides)), bundler configs and feature-flag defaults combine layered settings.
const defaults = { theme: { color: 'blue', spacing: { unit: 8 } }, plugins: ['a'], debug: false };
const user = { theme: { spacing: { unit: 4 } }, plugins: ['b'], debug: undefined };
deepMerge(defaults, user);
// {
// theme: { color: 'blue', spacing: { unit: 4 } },
// plugins: ['b'], // arrays are replaced, not merged
// debug: false, // undefined in a later object does not overwrite
// }
Signature
deepMerge(...objects) // returns a new plain object
Requirements
- Process the arguments left to right; later values win.
- If both the current value and the incoming value are plain objects (prototype
Object.prototypeornull), merge them recursively. - Otherwise the incoming value replaces the current one. In particular:
- Arrays are replaced, not merged by index or concatenated. (lodash's
mergemerges arrays by index; we deliberately don't.) - A plain object replaces a primitive or array, and vice versa.
- Arrays are replaced, not merged by index or concatenated. (lodash's
- An incoming value of
undefinedis skipped (the existing value is kept).nulldoes overwrite. - No mutation: none of the inputs is modified, and the result shares no plain objects or arrays with the inputs (they are copied). Other objects (
Date,Map, class instances, functions) are copied by reference. null/undefinedarguments are ignored. With no arguments, return{}.- Only own enumerable string keys are merged. Skip the key
__proto__(prototype-pollution guard).
Follow-up
- Allow a custom array strategy (
'replace' | 'concat' | 'byIndex'). - How would
deepMergebehave with circular references, and how would you fix it? - Why is a naive
mergea common source of prototype-pollution CVEs?
Spotted a mistake or a rights concern? Report an issue