- Mastering Immutable.js
- Adam Boduch
- 174字
- 2021-07-08 10:30:10
Adding multiple map key-value pairs
You can follow the same approach with maps when you need to add more than one value at the same time. Instead of chaining together the push() calls as you would with a list, you call set():
const myMap = Map.of(
'one', 1,
'two', 2,
'three', 3
);
const myChangedMap = myMap
.set('four', 4)
.set('five', 5)
.set('six', 6);
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,
// -> five: 5, six: 6 }
The first two calls to set() create intermediary maps, while the last call to set() produces the final map that ends up in myChangedMap.
The set() method is used to set new map values and to update existing map values. This poses a problem if you don't want to set a map value for a key that already exists. You can check if a given key already exists in your map using the has() method.
推薦閱讀
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- 深度學習經典案例解析:基于MATLAB
- Machine Learning with R Cookbook(Second Edition)
- Learning SAP Analytics Cloud
- 三維圖形化C++趣味編程
- 樂高機器人設計技巧:EV3結構設計與編程指導
- 秒懂設計模式
- 精通Python自然語言處理
- Spring Boot進階:原理、實戰與面試題分析
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Learning Continuous Integration with TeamCity
- Django 5企業級Web應用開發實戰(視頻教學版)
- C#面向對象程序設計(第2版)
- Go Systems Programming
- Unity 5 Game Optimization