mergeDeep()

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., Maps, Records, and objects), indexed (e.g., Lists and arrays), or set-like (e.g., Sets). 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

Discussion

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
This documentation is generated from immutable.d.ts. Pull requests and Issues welcome.