Merge
|
This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics. |
Merge( object1, object2, [customResolver] )
merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
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 |
|---|---|---|
|
Object |
The first object to merge with the second object. |
|
Object or Array of Objects. |
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' }{'a': 'Apple', 'b': 'Banana', 'x': 'width', 'y': 'height'}map[a:Apple b:Banana x:width y:height]ObjectV(a: StringV(Apple),b: StringV(Banana),x: StringV(width),y: StringV(height)){a: "Apple", b: "Banana", x: "width", y: "height"}{ a: 'Apple', b: 'Banana', x: 'width', y: 'height' } -
The following query demonstrates a merge when there is a key conflict and no
resolverfunction has been provided:{ f: 'Fauna' }{'f': 'Fauna'}map[f:Fauna]ObjectV(f: StringV(Fauna)){f: "Fauna"}{ f: 'Fauna' } -
The following query demonstrates that the default resolver performs a shallow merge:
{ data: { name: 'Fauna', extra: 'two' } }{'data': {'name': 'Fauna', 'extra': 'two'}}map[data:map[extra:two name:Fauna]]ObjectV(data: ObjectV(name: StringV(Fauna),extra: StringV(two))){data: {name: "Fauna", extra: "two"}}{ data: { name: 'Fauna', extra: 'two' } }This happens because only the
datafield is evaluated, since it is the only top-level field in each object to be merged, and the default resolver simply uses the value fordatainobject2for the result. -
The following query demonstrates a merge when there is a key conflict and a custom
resolverfunction has been provided:{ c: 'Compare', d: 'Delta' }{'c': 'Compare', 'd': 'Delta'}map[c:Compare d:Delta]ObjectV(c: StringV(Compare),d: StringV(Delta)){c: "Compare", d: "Delta"}{ 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' }{'t': 'turkey', 'a': 'Apple', 'b': 'Banana', 'c': 'Correlate', 'd': 'disparity'}map[a:Apple b:Banana c:Correlate d:disparity t:turkey]ObjectV(t: StringV(turkey),a: StringV(Apple),b: StringV(Banana),c: StringV(Correlate),d: StringV(disparity)){t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}{ 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: 1566580631370000, data: { letter: 'V', extra: '22nd' }, x: 10 }{'ref': Ref(id=122, collection=Ref(id=Letters, collection=Ref(id=collections))), 'ts': 1566580631370000, 'data': {'letter': 'V', 'extra': '22nd'}, 'x': 10}map[data:map[extra:22nd letter:V] ref:{122 0xc00013a2a0 0xc00013a2a0 <nil>} ts:1603747233950000 x:10]ObjectV(ref: RefV(id = "122", collection = RefV(id = "Letters", collection = RefV(id = "collections"))),ts: LongV(1603756505540000),data: ObjectV(letter: StringV(V),extra: StringV(22nd)),x: LongV(10)){ref: ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ts: 1566580631370000, data: {letter: "V", extra: "22nd"}, x: 10}{ ref: Ref(Collection("Letters"), "122"), ts: 1624310468410000, data: { letter: 'V', extra: '22nd' }, x: 10 } -
The following query demonstrates a merge with the
datafield of a document:{ letter: 'V', extra: '22nd', x: 10 }{'letter': 'V', 'extra': '22nd', 'x': 10}map[extra:22nd letter:V x:10]ObjectV(letter: StringV(V),extra: StringV(22nd),x: LongV(10)){letter: "V", extra: "22nd", x: 10}{ letter: 'V', extra: '22nd', x: 10 } -
The following query demonstrates a merge situation that provides no benefit:
{}{}map[]ObjectV(){}{}The
customResolveralways chooses the value fromobject1. Since there are no fields inobject1, the value returned fromcustomResolveris alwaysnull. Thenullreturn value means that all of the fields inobject2are removed fromobject1, which results in an empty object.