Like merge()
, but when two compatible collections are encountered with
the same key, it merges them as well, recursing deeply through the nested
data. Two collections are considered to be compatible (and thus will be
merged together) if they both fall into one of three categories: keyed
(e.g., Map
s, Record
s, and objects), indexed (e.g., List
s and
arrays), or set-like (e.g., Set
s). If they fall into separate
categories, mergeDeep
will replace the existing collection with the
collection being merged in. This behavior can be customized by using
mergeDeepWith()
.
mergeDeep<C>(collection: C,
...collections: Array<Iterable<unknown> | Iterable<[unknown, unknown]> | {[key: string]: unknown}>): C
Note: Indexed and set-like collections are merged using
concat()
/union()
and therefore do not recurse.
A functional alternative to collection.mergeDeep()
which will also work
with plain Objects and Arrays.
const { mergeDeep } = require('immutable')
const original = { x: { y: 123 }}
mergeDeep(original, { x: { z: 456 }}) // { x: { y: 123, z: 456 }}
console.log(original) // { x: { y: 123 }}run it