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

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.
主站蜘蛛池模板: 德令哈市| 辛集市| 永寿县| 临海市| 拉孜县| 锡林浩特市| 德格县| 沙湾县| 克什克腾旗| 乐安县| 清镇市| 鲁山县| 万载县| 揭阳市| 拉萨市| 赫章县| 翁源县| 崇信县| 吉安县| 进贤县| 府谷县| 黔东| 长沙市| 安丘市| 潮安县| 巴东县| 岳阳市| 冕宁县| 巩留县| 麻江县| 雷山县| 唐河县| 资溪县| 吉安市| 莲花县| 嘉峪关市| 息烽县| 霞浦县| 深州市| 北流市| 铜山县|