官术网_书友最值得收藏!

  • 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.
主站蜘蛛池模板: 保山市| 大埔县| 镇宁| 滕州市| 加查县| 将乐县| 灯塔市| 东莞市| 望城县| 开原市| 云南省| 连州市| 利川市| 宾阳县| 马关县| 彭山县| 郑州市| 抚顺县| 碌曲县| 德阳市| 偏关县| 中西区| 乌兰县| 兴仁县| 屯昌县| 鹤岗市| 井陉县| 阆中市| 上林县| 丰城市| 天峨县| 清远市| 托克逊县| 天等县| 宣武区| 淮滨县| 二手房| 博爱县| 赤水市| 西乌珠穆沁旗| 新巴尔虎左旗|