- Mastering Immutable.js
- Adam Boduch
- 117字
- 2021-07-08 10:30:10
Adding key-value pairs to maps
With maps, we use the set() method to add new values. Like the push() method used with lists, set() results in a new map:
const myMap = Map.of(
'one', 1,
'two', 2,
'three', 3
);
const myChangedMap = myMap.set('four', 4);
console.log('myMap', myMap.toJS());
// -> myMap { one: 1, two: 2, three: 3 }
console.log('myChangedMap', myChangedMap.toJS());
// -> myChangedMap { one: 1, two: 2, three: 3, four: 4 }
You have to supply the new key for the map, which can be any type of value, not just strings, along with the value itself. This results in the new map being stored in myChangedMap, which has the key-value pair that you've just set.
推薦閱讀
- Learning Neo4j
- 數據庫原理及應用(Access版)第3版
- 微服務與事件驅動架構
- Mastering OpenCV Android Application Programming
- 劍指JVM:虛擬機實踐與性能調優
- Scala Design Patterns
- Mastering Articulate Storyline
- 算法訓練營:提高篇(全彩版)
- C/C++程序員面試指南
- Python深度學習:模型、方法與實現
- Advanced Express Web Application Development
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Practical Maya Programming with Python
- 超好玩的Scratch 3.5少兒編程
- 軟件測試技術