JavaScript - Map() increment value

前端 未结 5 1572
说谎
说谎 2021-01-07 18:53

I have a map as follows:

let map = new Map();
map.set(\"a\", 1);
//Map is now {\'a\' => 1}

I want to change the value of a

5条回答
  •  情歌与酒
    2021-01-07 19:27

    According to the ECMAScript® 2015 Language Specification states, Map manipulation is based prototypes and the prototype methods assigned to add or retrieve data to or from a Map are the set and get methods respectively.

    Except for the unnecessary parenthesis around your map.get("a"), your code is perfectly okay. That's how the Map is meant to be used. If you are looking for something that "may" reduce the length of your code and if it does works for your specific requirement, you may use the JavaScript Object.

    So dear, your code is just the same as this:

    map.set("a", map.get("a")+1);
    

提交回复
热议问题