Merge
Merge( object1, object2, [customResolver] )
Description
The Merge
function combines two or more objects into one, by
performing a shallow merge of the fields and values from both objects
into a new object.
The merge starts with a copy of object1
— called result
— and then
merges object2
into result
, evaluating each top-level field in turn.
If object2
is an array of objects to merge, each object within
object2
is merged into result
in turn.
During the merge, each top-level field of the object to be merged into
result
is processed by a resolver function, which determines which
value to use in result
when there is a common field. The default
resolver always decides to use the value from the object to be merged,
overriding the value in result
.
You can provide an optional customResolver
function that overrides the
default resolver, and is used to determine which value to use.
When the resolver returns a null
value, Merge
deletes that
field in result
.
Parameters
Parameter | Type | Definition and Requirements |
---|---|---|
|
The first object to merge with the second object. |
|
|
The second object, or array of objects, to merge with the first
object. If an array of objects is provided, each object in the array
is merged with |
|
|
Optional - A lambda function that replaces the default resolver and decides which value to use during the merge. The
where If |
Returns
An object that is the result of merging object1
with object2
(and
any additional objects that you may have provided).
Examples
-
The following query demonstrates a merge of two objects where there is no key conflict:
{ a: 'Apple', b: 'Banana', x: 'width', y: 'height' }
-
The following query demonstrates a merge when there is a key conflict and no
resolver
function has been provided:{ f: 'Fauna' }
-
The following query demonstrates that the default resolver performs a shallow merge:
{ data: { name: 'Fauna', extra: 'two' } }
This happens because only the
data
field is evaluated, since it is the only top-level field in each object to be merged, and the default resolver simply uses the value fordata
inobject2
for the result. -
The following query demonstrates a merge when there is a key conflict and a custom
resolver
function has been provided:{ c: 'Compare', d: 'Delta' }
-
The following query demonstrates a merge when an array of objects is provided:
{ t: 'turkey', a: 'Apple', b: 'Banana', c: 'Correlate', d: 'disparity' }
-
The following query demonstrates a merge with a document:
{ ref: Ref(Collection("Letters"), "122"), ts: 1624310468410000, data: { letter: 'V', extra: '22nd' }, x: 10 }
-
The following query demonstrates a merge with the
data
field of a document:{ letter: 'V', extra: '22nd', x: 10 }
-
The following query demonstrates a merge situation that provides no benefit:
{}
The
customResolver
always chooses the value fromobject1
. Since there are no fields inobject1
, the value returned fromcustomResolver
is alwaysnull
. Thenull
return value means that all of the fields inobject2
are removed fromobject1
, which results in an empty object.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!