'javascript hashmap one line modify or create
I want to know if there is a clean way to modify a value in a hashmap or create it if it doesn't exist without doing an if block. Example of what i'm currently doing
let dict = {}
if(dict['key']){
dict['key'] += 1
} else {
dict['key'] = 1
}
Want to know if there is a cleaner way to do what I did above.
Solution 1:[1]
Use dot notation, because the property isn't dynamic, and alternate dict.key with 0 before adding 1.
dict.key = (dict.key || 0) + 1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CertainPerformance |
