Moretai
Moretai

Reputation: 151

how to merge nested maps in immutable.js

import {Map} from 'immutable'

let map1 = Map({a:1,b:{c:2,d:3,e:1});
let map2 = Map({c:100,d:400});

how do I get Map {a:1,b:{c:100,d:400,e:1}} by merging map1 and map2?

Upvotes: 1

Views: 724

Answers (1)

Matt  Watson
Matt Watson

Reputation: 5317

You can use immutable.js built in mergeIn function. You will have to tweak map1 slightly so that the nested Map is also an instance of Map

The following code does the job:

let map1 = Map({ a: 1, b: Map({ c: 2, d: 3, e: 1 })})
let map2 = Map({ c: 100, d: 400 })
let map3 = map1.mergeIn(['b'], map2)

Upvotes: 2

Related Questions