There can be situations where we want to merge nested properties into an object. For that, we can use mergeDeep
functions. There are 2 functions, namely, mergeDeepLeft
and mergeDeepRight
. They overwrite the primitive values, but instead of overwriting the nested objects, they recursively merge the nested objects.
To differentiate between the two mergeDeep
functions, we can think as follows. For mergeDeepLeft
the right side values are overwritten by the left side values if they are of primitive data types and vice versa in mergeDeepRight
.
Let's look at both mergeDeepLeft
and mergeDeepRight
with an example.
mergeDeepLeft
As you can see, the id
key got replaced by the value from the left (or first) object changes
while customer
key of order
didn't get fully overwritten, but rather customer.email
got added, and customer.name
from order
got overridden by the value from changes
, and the rest of the properties from order
were retained as it was.
mergeDeepRight
As you can see, the value of the id
key was taken from the right (changes
) object. customer.isAdmin
which is present in the changes
object got added and the value for customer.name
was taken from changes
.