- Mastering Immutable.js
- Adam Boduch
- 88字
- 2021-07-08 10:30:12
Removing values from maps
Maps have a remove() method that works the same way as the list version. The only difference is that it takes a key as the argument instead of an index:
const myMap = Map.of(
'one', 1,
'two', 2,
'three', 3
);
const myChangedMap = myMap.remove('one');
console.log('myMap', myMap.toJS());
// -> myMap { one: 1, two: 2, three: 3 }
console.log('myChangedMap', myChangedMap.toJS());
// -> myChangedMap { three: 3, two: 2 }
By calling remove('one') on myMap, you get myChangedMap, a new map without the removed key.