- 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.
推薦閱讀
- Mastering Entity Framework Core 2.0
- 深入理解Django:框架內(nèi)幕與實現(xiàn)原理
- Vue.js 3.x從入門到精通(視頻教學(xué)版)
- Vue.js快跑:構(gòu)建觸手可及的高性能Web應(yīng)用
- FFmpeg入門詳解:音視頻原理及應(yīng)用
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- jQuery炫酷應(yīng)用實例集錦
- 51單片機(jī)C語言開發(fā)教程
- Unity 2018 Augmented Reality Projects
- Mastering Adobe Captivate 7
- Mastering Leap Motion
- Flink入門與實戰(zhàn)
- Java EE實用教程
- LabVIEW數(shù)據(jù)采集(第2版)
- 前端程序員面試筆試真題與解析