merge
Learn how to 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.
Atomicity
If the value at the specified path is not a collection, the operation fails and the entire patch is discarded.
Signature
This method has the following signature:
patch.merge(path: string, value: JsonArray | JsonMap): PatchThe 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 value | Given value | Result |
|---|---|---|
| {} | {} | {} |
| {} | {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: