Merge Deep

3.0.3 · active · verified Sun Apr 19

merge-deep is a JavaScript utility designed to recursively merge the properties of one or more source objects into a target object. It is currently at stable version 3.0.3, maintaining a mature and relatively stable codebase. The library's core functionality is to perform a deep merge, meaning it traverses nested objects and combines their properties rather than simply overwriting them, which is a common behavior in shallow merge operations. This ensures a comprehensive union of object structures. It draws its implementation foundation from the `mout` library's merge utility. While a specific release cadence isn't explicitly defined, the project typically receives updates for maintenance or minor enhancements. A key differentiator is its commitment to a pure function approach, aiming to return a new merged object without directly mutating the input objects, providing a predictable and side-effect-free way to combine complex configurations or data structures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `merge-deep` to perform a basic deep merge on two objects and then on multiple objects into a new target.

const merge = require('merge-deep');

const obj1 = {
  a: {
    b: {
      c: 'c1',
      d: 'd1'
    },
    x: 1
  }
};

const obj2 = {
  a: {
    b: {
      e: 'e2',
      f: 'f2'
    },
    y: 2
  },
  z: 3
};

// Basic deep merge of two objects
const merged = merge(obj1, obj2);
console.log('Merged two objects:', merged);
// Expected: { a: { b: { c: 'c1', d: 'd1', e: 'e2', f: 'f2' }, x: 1, y: 2 }, z: 3 }

// Demonstrate merging multiple objects into a new empty object
const obj3 = { a: { b: { g: 'g3' } } };
const mergedMany = merge({}, obj1, obj2, obj3);
console.log('Merged multiple objects:', mergedMany);
// Expected to deeply merge properties from obj1, obj2, obj3 into a new empty object.

view raw JSON →