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
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
Share Improve this question edited Jul 18, 2017 at 5:51 Ajay Brahmakshatriya 9,2333 gold badges29 silver badges51 bronze badges asked Jul 18, 2017 at 2:33 MoretaiMoretai 1511 silver badge8 bronze badges 3- sorry,I‘m new to stackoverflow and immutable – Moretai Commented Jul 18, 2017 at 2:34
- Please post your code as text. Images of code are relatively useless, and inaccessible for some. – castletheperson Commented Jul 18, 2017 at 2:42
- hello.for example. let map1 = Map({a:1,b:{c:2,d:3,e:1}); let map2 = Map({c:100,d:400}); how to get Map {a:1,b:{c:100,d:400,e:1}} – Moretai Commented Jul 18, 2017 at 2:44
2 Answers
Reset to default 7const { 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);
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./refayef/2/edit?js,console