- 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.
推薦閱讀
- Instant Testing with CasperJS
- JavaScript修煉之道
- Python量化投資指南:基礎、數據與實戰
- 劍指Offer(專項突破版):數據結構與算法名企面試題精講
- Building a Quadcopter with Arduino
- 西門子S7-200 SMART PLC編程從入門到實踐
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Spring Boot實戰
- 計算機應用基礎項目化教程
- Exploring SE for Android
- Application Development with Swift
- Mastering OpenStack
- Python編程入門(第3版)
- Test-Driven iOS Development with Swift
- Microsoft Dynamics GP 2013 Cookbook