merge

Merge two maps or lists.

This method chains a merge operation to the patch, combining entries in maps or appending items to a list.

Non-existent paths and null values are treated as an empty collection.

Signature

This method has the following signature:

patch.merge(path: string, value: JsonArray | JsonMap): Patch

The return is the Patch instance itself to allow operation chaining.

Example

Here is a basic example of how to use this method:

patch.merge('preferences', {color: 'green'});

The following table shows how the operation behaves in different scenarios:

Current valueGiven valueResult
{}{}{}
{}{a: 1}{a: 1}
{a: 1}{b: 2}{a: 1, b: 2}
{a: 1}{a: 2}{a: 2}
null{a: 1}{a: 1}
null[1][1]
1[2][1, 2]
[][1][1]
[1][2][1, 2]
['a', 'b']['b', 'c']['a', 'b', 'b', 'c']

Parameters

The following list describes the supported parameters:

path
string

The path to the collection in dot notation, like flags.

value
JSON

The value to merge with the Current value, like {'enabled': true}.