- 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.
推薦閱讀
- 現代C++編程:從入門到實踐
- Mastering NetBeans
- TypeScript入門與實戰
- Mastering SVG
- Learning Informatica PowerCenter 10.x(Second Edition)
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- SharePoint Development with the SharePoint Framework
- INSTANT Passbook App Development for iOS How-to
- 少兒編程輕松學(全2冊)
- ASP.NET本質論
- Java Script從入門到精通(第5版)
- Learning Node.js for Mobile Application Development
- Office VBA開發經典:中級進階卷
- Java EE框架開發技術與案例教程
- 算法學習與應用從入門到精通