Deepmerge
This component addresses the problem to merge a nested, and sometimes complicated, object structure, with one ore many others objects.
const
obj1 = { foo:{ bar:'baz' } },
obj2 = { foo:{ baz:'qux' } },
deepmerge = core.locate('core/deepmerge')
deepmerge.merge(obj1, obj2)
In above example, after merge, obj1
will hold the following value.
{
foo:
{
bar:'baz',
baz:'qux'
}
}
As shown by the example above, the merge
method extends the object obj1
with the attributes from obj2
, without replacing the foo
reference in obj1
with the foo
reference in obj2
.
Merge arrays
Deepmerge inclusive will extend an array found in one object with the values in another array of the other object, as shown in the example below.
const
obj1 = { foo:['bar'] },
obj2 = { foo:['baz'] },
deepmerge = core.locate('core/deepmerge')
deepmerge.mergeInclusive(obj1, obj2)
In above example, after merge, obj1
will hold the following value.
{
foo: ['bar','baz']
}
Merge multiple objects
It is possible to merge multiple objects at the same time, as the example below shows.
const
obj1 = { foo:{ foo:'baz' } },
obj2 = { foo:{ bar:'qux' } },
obj3 = { bar:{ baz:'qux' } },
deepmerge = core.locate('core/deepmerge')
deepmerge.merge(obj1, obj2, obj3)
In above example, after merge, obj1
will hold the following value.
{
foo:
{
foo:'baz',
bar:'qux'
},
bar:
{
baz:'qux'
}
}
In the above example 3 different objects are merged. There is no limit by the component how many objects you can merge at the same time.
Merge conflicts
If a conflict in the merge is present, then the last object in the merge will be prioritized as the resulting value.
const
obj1 = { foo:'bar' },
obj2 = { foo:'baz' },
deepmerge = core.locate('core/deepmerge')
deepmerge.merge(obj1, obj2)
In above example, after merge, obj1
will hold the following value.
{
foo:'baz'
}
As the example above shows, the foo
reference was present in both objects. A string can not be extended by the core/deepmerge
component, and will therefor replace the value.