Deep Merge Objects
Deep Merge Objects is a reusable JavaScript snippet. Recursively merges two or more objects. Unlike Object.assign(), this handles nested objects by merging rather than replacing them. The full implementation: function deepMerge(target, ...sources) { if (!sources.length) return target const source = sources.shift() if (source !== undefined) { for (const key in source) { if (source.hasOwnProperty(key)) { if (source[key] instanceof Object && target[key] instanceof Object) { deepMerge(target[key], source[key]) } else { target[key] = source[key] } } } } return deepMerge(target, ...sources) } // Usage const a = { x: 1, y: { a: 1 } } const b = { y: { b: 2 }, z: 3 } deepMerge({}, a, b)… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.