Mastering Immutable.js
上QQ阅读APP看书,第一时间看更新

Setting map values

The set() method for maps is used to add and to change collection values. If the key that's being set doesn't exist, then the key-value pair is set for the first time. If the key does exist, then the previous value of that key is replaced with the new value, as follows:

const myMap = Map.of('one', 1);
const myChangedMap = myMap.set('one', 'one');

console.log('myMap', myMap.toJS());
// -> myMap { one: 1 }
console.log('myChangedMap', myChangedMap.toJS());
// -> myChangedMap { one: 'one' }

Calling set() produces a new map, because all Immutable.js mutations are persistent changes.