What is the use of the Map.has() method in JavaScript?

The Map.has() method in JavaScript is used to check if a Map object contains a specified key or not. The method returns a boolean value true if the specified key exists in the Map, otherwise, it returns false.

Here’s an example of how to use Map.has() method:

// Create a new Map object
let map = new Map();

// Add key-value pairs to the Map
map.set("key1", "value1");
map.set("key2", "value2");
map.set("key3", "value3");

// Check if the Map contains a specific key
console.log(map.has("key1")); // Output: true
console.log(map.has("key4")); // Output: false

In the example above, we created a new Map object and added some key-value pairs to it using the set() method. Then, we used the has() method to check if the Map contains the keys key1 and key4. Since the key key1 exists in the Map, the first console.log() statement outputs true. However, the key key4 doesn’t exist in the Map, so the second console.log() statement outputs false.

The Map.has() method can also be used in combination with other methods to perform various operations on the Map object.

Here are some examples:

Example 1: Looping through a Map using the has() method:

// Create a new Map object
let map = new Map();

// Add key-value pairs to the Map
map.set("key1", "value1");
map.set("key2", "value2");
map.set("key3", "value3");

// Loop through the Map and log the keys and values
for (let [key, value] of map) {
  if (map.has(key)) {
    console.log(`${key} = ${value}`);
  }
}

In the example above, we created a Map object and added some key-value pairs to it. Then, we used a for...of loop to iterate through the Map and log the keys and values. Inside the loop, we checked if the Map contains the current key using the has() method. If the key exists, we logged the key-value pair to the console.

Example 2: Removing a key-value pair from a Map using the has() method:

// Create a new Map object
let map = new Map();

// Add key-value pairs to the Map
map.set("key1", "value1");
map.set("key2", "value2");
map.set("key3", "value3");

// Remove a key-value pair from the Map
if (map.has("key2")) {
  map.delete("key2");
}

// Check if the key-value pair was removed
console.log(map.has("key2")); // Output: false

In the example above, we created a Map object and added some key-value pairs to it. Then, we used the has() method to check if the Map contains the key key2. If the key exists, we used the delete() method to remove the key-value pair from the Map. Finally, we checked if the key-value pair was removed by using the has() method again.

Example 3: Using has() method to avoid key duplication in a Map:

// Create an array of objects
let data = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Bob" },
  { id: 2, name: "Alice" },
];

// Convert the array into a Map object
let map = new Map();
for (let item of data) {
  if (!map.has(item.id)) {
    map.set(item.id, item.name);
  }
}

// Log the contents of the Map
for (let [key, value] of map) {
  console.log(`${key} = ${value}`);
}

In the example above, we created an array of objects called data with four items, two of which have the same id value. We want to create a Map object using the id property of each object as the key. Inside the loop, we use the has() method to check if the current id value already exists in the Map. If the key doesn’t exist, we add a new key-value pair to the Map using the set() method. Finally, we log the contents of the Map to the console to verify that there are no duplicate keys.

In conclusion, the Map.has() method is an important tool in working with Map objects in JavaScript. It allows us to quickly check if a specified key exists in the Map and perform various operations based on the result.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: