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

  • 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.
主站蜘蛛池模板: 屏山县| 泾源县| 安达市| 含山县| 双鸭山市| 兰坪| 沂水县| 宝清县| 岗巴县| 巴东县| 巨野县| 平陆县| 拉萨市| 唐海县| 通城县| 平昌县| 北票市| 天门市| 安顺市| 离岛区| 南乐县| 青岛市| 合作市| 讷河市| 曲沃县| 华安县| 定陶县| 丹阳市| 新安县| 怀远县| 苏尼特左旗| 莱芜市| 承德县| 凤庆县| 雷州市| 偃师市| 满洲里市| 思南县| 南澳县| 特克斯县| 白水县|