add

Add a value to a collection.

This method chains an add operation to the patch.

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

Signature

This method has the following signature:

patch.add(path: string, element: JsonValue): Patch

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

Example

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

path.add('searches', 'data science');

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

Current valueGiven valueResult
null'a'['a']
'a'null['a']
'a''b'['a', 'b']
['a']null['a']
[]'a'['a']
['a', 'b']'a'['a', 'b', 'a']

Parameters

The following list describes the supported parameters:

path
string

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

element
JSON

The element to add to the collection, like data science.