I have a map that consists of several key : value pairs, and the keys are all integers (that are then of course stored as strings).
However, I can't use Map.prototype.has("1") nor Map.prototype.has(1) to confirm a key exists within the map. How do I go about doing this? I want to use the Map.prototype.has() method in order to avoid the whole 0 is false problem.
let map = new Map();
map[1] = 2;
console.log(map); //Map { 1: 2 }
console.log(map.has("1")); //false
console.log(map.has(1)); //false
When you do
map[1] = 2;you're not setting the item in the map data structure itself, but on the underlying generic object. Therefore, you can't expect map related methods ashas()andget()to return you "correct" results as the map structure is actually empty. Always set map properties withmap.set().Also, note that
Mapdoesn't support indexing. You can't get an item by doingmap[key]. This, once again, will access a property from the underlying object. You have to usemap.get()instead.You can see the difference by doing this:
[[Entries]]is the actual map structure, everything else is coming from the object.For completeness, object properties can only be strings or symbols. Map, on the other hand, support all types(including objects and functions). I am saying this because if you do:
you actually mutate the same property(
'1', numeric keys are actually converted to strings). If you use the actual map structure, however:You have two separate entries:
'1'and1.