Merge two or more objects deeply.

Methods

(inner) all(array, options) → {Array|object}

Merges an array of arrays or objects using deepMerge into one array or object.

Parameters:
NameTypeDescription
arrayArray

The array of arrays or objects to deep merge.

optionsobject

Options object.

Returns:

Returns a new array or object, after merging all items in the array.

Type: 
Array | object
Example
import { deepMerge } from "@gravityforms/utils";

function Example() {
	const array = [
		{
			foo: { bar: 3 },
			array: [ 1, 2, 3 ],
		},
		{
			foo: { baz: 4, bar: 5 },
			quux: 5,
			array: [ 4, 5, 6 ],
		},
		{
			foo: { baz: 1 },
			array: [ 7, 8, 9 ],
		},
	];

	const output = deepMerge.all( array );
}

(inner) deepMerge(target, source, options) → {Array|object}

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.

Taken and adapted from https://www.npmjs.com/package/deepmerge

Parameters:
NameTypeDescription
targetArray | object

The original array or object to merge to.

sourceArray | object

The new array or object that will get priority when keys match.

optionsobject

Options object.

Since
  • 1.0.0
Requires:
Returns:

Returns a new array or object, after merging target and source.

Type: 
Array | object
Example
import { deepMerge } from "@gravityforms/utils";

function Example() {
	const target = {
		foo: { bar: 3 },
		array: [ 1, 2, 3 ]
	};

	const source = {
		foo: { baz: 4, bar: 5 },
		quux: 5,
		array: [ 4, 5, 6 ],
	};

	const output = deepMerge( target, source );
}