Reputation: 151
I want to get map like below:
Map{
"status":1,
"user":{
"name":"more",
"age":8,
"id":1
}
}
My full code is:
import {fromJS} from 'immutable'
let b={
"user":{
name:"more11",
age:8
},
"status":1
}
let d={
"id":1,
"name":"more",
"age":19
}
let d1=fromJS(d)
let b2=fromJS(b)
Const map2=b1.get('user).mergeDeepIn(d1)
console.log(map2.getIn(['user','name']));
console.log(map2.getIn(['user','age']));
Original code image
Upvotes: 4
Views: 6869
Reputation: 1784
Since your map1 is nested, and map1 only matches a nested section of it, there's no way to automatically merge them without actually pointing what goes where.
Here's an example using the object spread operator to merge the underlying map objects:
import { Map } from 'immutable'
const map1 = new Map({
a: 1,
b: {
c: 2,
d: 3,
e:1
},
})
const map2 = new Map({
c: 100,
d: 400
})
const map3 = new Map({
...map1.toObject(),
b: {
...map1.get('b'),
...map2.toObject(),
}
})
console.log(
map3.toObject() // Map { a: 1, b: { c: 100, d: 400, e: 1} }
)
The same can be achieved in a more concise way using Ramda:
import R from 'ramda'
const map4 = new Map(
R.evolve({ b: R.merge(R.__, map2.toObject()) }, map1.toObject())
)
Demo: http://jsbin.com/refayef/2/edit?js,console
Upvotes: 0
Reputation: 151
const { Map } = require('immutable')
let map1 = Map({a:1,b:{c:2,d:3,e:1}});
let map2 = Map({c:100,d:400});
let map3 = Map(map1.get('b'));
let map4 = map3.merge(map2);
let map5 = map1.set('b',map4);
console.log(map5);
Upvotes: 7