If I were to store the array information in key/value pairs in a regular object, as I would fill out the object each key must be either the string or symbol data type, so each element of my array of numbers is transformed into a string as it is made into a key in the object. However, the Map object doesn’t force keys to be either strings or symbols, and so the keys can remain numbers, thus allowing for mathematical operations to be performed on them without any additional parsing. I definitely enjoy being able to use the keys of a map object without having to remember to change them into numbers, but I have also found that that actually accessing some of the information in a map, and then, as the title mentions, incrementing values can be somewhat tedious, as the dot/bracket notation available to regular objects, is replaced by get and set methods that can be unwieldy to work with.
A common practice for algorithmic problems is to increment values of a key value pair. Initially, my approach to doing this with a map object just felt off, and it seemed like there had to be a better way.
There’s really nothing wrong with incrementing a value using the above method, but it does come across as repetitive/wordy, and I was interested to find out what other options might be available.
It turns out that there is a simple solution that allows to update the value of a key in a map without having to use the set method with the get method inside of it.
By initially setting the value of the apples inside of an object, I am able to access and then directly update the value without any additional function calls.
Frankly, I don’t think that there is really any true advantage to doing this one way versus another, but I do think that using the second method to update a value in a map looks cleaner, even if the mdn documentation doesn’t really make it clear that this is an acceptable way to do this. I’d be interested to learn more about if there are any performative differences between these two methods, but for my purposes right now, I would definitely continue to update values by using the second method.