JavaScript – Map() increment value

The way you do it is fine. That is how you need to do it if you are working with primitive values. If you want to avoid the call to map.set, then you must revert to a reference to a value. In other words, then you need to store an object, not a primitive:

let map = new Map();
map.set("a", {val: 1});

And then incrementing becomes:

map.get("a").val++;

Leave a Comment