書名: 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.
推薦閱讀
- 計算機網絡
- ExtGWT Rich Internet Application Cookbook
- Progressive Web Apps with React
- Visual Studio 2012 Cookbook
- 潮流:UI設計必修課
- Redis Applied Design Patterns
- Xcode 7 Essentials(Second Edition)
- Java入門很輕松(微課超值版)
- C和C++安全編碼(原書第2版)
- Developing Middleware in Java EE 8
- Servlet/JSP深入詳解
- Java Web程序設計
- Android開發:從0到1 (清華開發者書庫)
- Learning FuelPHP for Effective PHP Development
- Python程序設計與算法基礎教程(第2版)(微課版)