TIP: When using console.log
while debugging Immutable.js objects, you’ll see weird Map structures and it doesn’t help very much in terms of figurring out object structure. For easier debugging, you can perform .toJS()
on an Immutable object and console.log that to see the object structure in plain JS.
1console.log(myMap) // console shows weird Immutable.js object
2console.log(myMap.toJS()) // console shows plain JS object
1const Immutable = require('immutable')
2
3// Maps are like JS Objects
4
5var map1 = Immutable.Map({ a: 1, b: 2, c: 3 })
6
7console.info('map1', map1) // map1 Map { "a": 1, "b": 2, "c": 3 }
8
9map1
10 .set()
11 .get()
12
13// To set values, you always create a new instance of whatever you're editing
14var map2 = map1.set('b', 50);
15console.info('map2', map2)
Values in Immutable.js data structures can’t be mutated, so with all the setter methods, you can’t just do
1map1.set('a': 100)
it’d do nothing, because you can’t directly set (i.e. change) the map. You have to set the value on a new instance.
1var map1 = map1.set('a': 100)