How to increment a value in a JavaScript object?

前端 未结 7 1016
时光说笑
时光说笑 2021-02-12 13:50
var map = {};
map[key] = value;

How can I

  • assign value 1 if key does not yet exist in the object
  • increment the value by 1 if it
7条回答
  •  遥遥无期
    2021-02-12 14:07

    Recently it could be

    map[key] = (map[key] ?? 0) + 1;
    

    Nullish coalescing operator

提交回复
热议问题