admin管理员组

文章数量:1303668

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

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

Share Improve this question edited Mar 16, 2021 at 22:07 dehuff asked Mar 16, 2021 at 20:44 dehuffdehuff 1151 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Use Map.prototype.set not map[1] = 2. Maps are Objects with their own set of rules, so you cannot set it the way above. Learn more here.

let map = new Map();
map.set(1,2);
console.log(map); // Map(1) { 1 => 2 }
console.log(map.has("1")); //false
console.log(map.has(1)); //true

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 as has() and get() to return you "correct" results as the map structure is actually empty. Always set map properties with map.set().

Also, note that Map doesn't support indexing. You can't get an item by doing map[key]. This, once again, will access a property from the underlying object. You have to use map.get() instead.

You can see the difference by doing this:

let testMap = new Map();

testMap['prop1'] = 'prop1value';
testMap.set('prop2', 'prop2value');
testMap.set('prop1', 'prop1value');

console.log(testMap);

[[Entries]] is the actual map structure, everything else is ing from the object.

For pleteness, 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:

let testMap = new Map();
    
testMap['1'] = 'prop1value';
testMap[1] = 'prop2value';

you actually mutate the same property('1', numeric keys are actually converted to strings). If you use the actual map structure, however:

let testMap = new Map();

testMap.set('1', 'prop1value');
testMap.set(1, 'prop2value');

You have two separate entries: '1' and 1.

本文标签: javascriptWhy does Maphas() return false for an Integer key that does existStack Overflow