Merge

Not available in this language yet.
Not available in this language yet.
Merge( object1, object2, [resolver] )
Merge( object1, object2, [resolver] )
Not available in this language yet.
Merge( object1, object2, [resolver] )

Description

The Merge function combines two or more objects into one, by merging the keys and values from both objects into a new object. The optional resolver function can be used to resolve conflicts when keys match.

Parameters

Argument Type Definition and Requirements

object1

The first object to merge with the second object.

object2

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 object1 in turn.

resolver

Optional - A lambda function that can resolve conflicts when keys in object1 match those in object2.

The resolver function has the signature (key, value1, value2) ⇒ newValue, where key is the name of the key that exists in both object1 and object2, and a and b are the respective values for that key.

The default resolution behaves like (key, a, b) ⇒ b, which means that whenever a conflict occurs, the value from object2 is used.

Returns

An object that is the result of merging object1 with object2.

Examples

The following query demonstrates a merge of two objects where there is no key conflict:

Not available in this language yet.
Not available in this language yet.
System.out.println(
  client.query(
    Merge(
      Obj("a", "Apple", "b", "Banana"),
      Obj("x", "width", "y", "height")
    )
  ).get());
client.query(
  q.Merge(
    { a: "Apple", b: "Banana" },
    { x: "width", y: "height" }
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
client.query(
  Merge(
    Obj("a" -> "Apple", "b" -> "Banana"),
    Obj("x" -> "width", "y" -> "height")
 )
)
{a: "Apple", b: "Banana", x: "width", y: "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 resolver function has been provided:

Not available in this language yet.
Not available in this language yet.
System.out.println(
  client.query(
    Merge(
      Obj("f", "First"),
      Obj("f", "Fauna")
    )
  ).get());
client.query(
  q.Merge(
    { f: "First" },
    { f: "Fauna" }
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
client.query(
  Merge(
    Obj("f" -> "First"),
    Obj("f" -> "Fauna")
 )
)
{f: "Fauna"}
{ f: 'Fauna' }
{f: "Fauna"}

The following query demonstrates a merge when there is a key conflict and a custom resolver function has been provided:

Not available in this language yet.
Not available in this language yet.
System.out.println(
  client.query(
    Merge(
      Obj("c", "Compare", "d", "Difference"),
      Obj("c", "Contrast", "d", "Delta"),
      Lambda(
        Arr("key", "a", "b"),
        If(
          Equals(Var("key"), "c"),
          Var("a"),
          Var("b")
        )
      )
    )
  ).get());
client.query(
  q.Merge(
    { c: "Compare", d: "Difference" },
    { c: "Contrast", d: "Delta" },
    q.Lambda(
      ["key", "a", "b"],
      q.If(
        q.Equals(q.Var("key"), "c"),
        q.Var("a"),
        q.Var("b")
      )
    )
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
println(Await.result(
  client.query(
    Merge(
      Obj("c" -> "Compare", "d" -> "Difference"),
      Obj("c" -> "Contrast", "d" -> "Delta"),
      Lambda(
        Arr("key", "a", "b"),
        If(
          Equals(Var("key"), "c"),
          Var("a"),
          Var("b")
        )
      )
    )
  ),
  5.seconds
))
{c: "Compare", d: "Delta"}
{ c: 'Compare', d: 'Delta' }
{c: "Compare", d: "Delta"}

The following query demonstrates a merge when an array of objects is provided:

Not available in this language yet.
Not available in this language yet.
System.out.println(
  client.query(
    Merge(
      Obj("c", "Compare", "d", "Difference"),
      Arr(
        Obj("c", "Contrast", "d", "Delta"),
        Obj("a", "Apple", "b", "Banana", "t", "Tomato"),
        Obj("c", "Correlate", "t", "turkey"),
        Obj("d", "disparity"),
      )
    )
  ).get());
client.query(
  q.Merge(
    { c: "Compare", d: "Difference", },
    [
      { c: "Contrast", d: "Delta", },
      { a: "Apple", b: "Banana", t: "Tomato" },
      { c: "Correlate", t: "turkey" },
      { d: "disparity" }
    ]
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
println(Await.result(
  client.query(
    Merge(
      Obj("c" -> "Compare", "d" -> "Difference"),
      Arr(
        Obj("c" -> "Contrast", "d" -> "Delta"),
        Obj("a" -> "Apple", "b" -> "Banana", "t" -> "Tomato"),
        Obj("c" -> "Correlate", "t" -> "turkey"),
        Obj("d" -> "disparity"),
      )
    )
  ),
  5.seconds
))
{t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}
{ t: 'turkey',
  a: 'Apple',
  b: 'Banana',
  c: 'Compare',
  d: 'disparity' }
{t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!