Borislav Hadzhiev
Reading timeยท3 min
Photo from Unsplash
Use the Map.has()
method to check if a key exists in a Map
object.
The has()
method returns true
if the specified key exists in the Map
,
otherwise, it returns false
.
const map = new Map(); map.set('name', 'bobbyhadz'); console.log(map.has('name')); // ๐๏ธ true console.log(map.has('age')); // ๐๏ธ false if (map.has('name')) { // ๐๏ธ this runs console.log('The `name` key exists in the Map object'); } else { console.log('The `name` key does NOT exist in the Map object'); }
The only parameter the
Map.has()
method takes is the key of the element to test for presence in the Map
.
The Map.has()
method returns a boolean result:
true
if the key exists in the Map
objectfalse
if the key doesn't exist in the Map
objecthas()
method returns true
if the key is set to undefined
, null
or any other falsy value.const map = new Map(); map.set('undef', undefined); map.set('null', null); map.set('nan', Number.NaN); console.log(map.has('undef')); // ๐๏ธ true console.log(map.has('null')); // ๐๏ธ true console.log(map.has('nan')); // ๐๏ธ true
The method checks for existence, not whether the value is truthy or falsy.
If a value gets deleted from the map, the has
method picks up the update
immediately.
const map = new Map(); map.set('name', 'Tom'); console.log(map.has('name')); // ๐๏ธ true map.delete('name'); console.log(map.has('name')); // ๐๏ธ false
We used the Map.delete()
method to remove an element from the Map
.
As expected, the call to the has()
method returned false
immediately after.
The same is the case when using the Map.set()
method to add an element to the
Map
or the map.clear()
method to remove all elements from the Map
.
const map = new Map(); console.log(map.has('name')); // ๐๏ธ false map.set('name', 'bobbyhadz'); console.log(map.has('name')); // ๐๏ธ true map.clear(); console.log(map.has('name')); // ๐๏ธ false
The Map.clear()
method removes all elements from a Map
object, so passing
any key to the Map.has()
method returns false
after a call to Map.clear()
.
To check if a Map
has an object key, call the has()
method, passing it a
reference to the object, e.g. map.has(obj)
.
The has()
method will return true
if the object key is contained in the
Map
and false
otherwise.
const obj = {country: 'Chile'}; const map1 = new Map([[obj, {city: 'Santiago'}]]); // โ ( BEST ) - With reference console.log(map1.has(obj)); // ๐๏ธ true console.log(map1.get(obj)); // ๐๏ธ {city: 'Santiago'} // -------------------------------------------------------- // โ If you don't have a reference, loop over the map to check let hasKey = false; for (const [key, value] of map1) { if (typeof key === 'object' && key.country === 'Chile') { hasKey = true; break; } } console.log(hasKey); // ๐๏ธ true
We used the
Map.has
method to check if a Map
has an object key.
has()
method.This wouldn't work if you pass an object containing the same key-value pairs because it would be stored at a different location in memory.
const obj = {country: 'Chile'}; const map1 = new Map([[obj, {city: 'Santiago'}]]); console.log(map1.has({country: 'Chile'})); // ๐๏ธ false
JavaScript objects are compared by reference, and not by their contents.
const obj = {country: 'Chile'}; // ๐๏ธ๏ธ false console.log({country: 'Chile'} === {country: 'Chile'}); // ๐๏ธ๏ธ true console.log(obj === obj);
If you don't have a reference to the object, use a
for...of
loop to iterate over the Map
and check for the existence of the object key.
const map1 = new Map([[{country: 'Chile'}, {city: 'Santiago'}]]); let hasKey = false; for (const [key, value] of map1) { if (typeof key === 'object' && key.country === 'Chile') { hasKey = true; break; } } console.log(hasKey); // ๐๏ธ true
The
for...of
statement is used to loop over iterable objects like arrays, strings, Map
,
Set
and NodeList
objects and generators
.
On each iteration, we check if the current key is an object and contains a
country
property that is equal to Chile
.
value
variable if you don't need it.After we have confirmed that the object key exists in the Map
, we use the
break
statement to exit the loop and avoid unnecessary work.
We used the logical AND (&&) operator, so for the if
block to run, both
conditions have to be met.
The key has to be an object and it has to have a country
property that is
equal to Chile
.
You can check for multiple properties and values to make your if
statement
more robust.