How To Check If A Map Has An Object Key Using JavaScript

Are you looking for a way to check if a map has an object key in JavaScript? Congratulations, today, we will show you a few ways to do that, such as using the has() method and creating the function checking. Read the article’s end to know how we can do it.

To Check If A Map Has An Object Key In JavaScript.

Using the has() method

First, we will introduce you to using the has() method to check if a map has an object key. This method usually checks if an element in a Map exists and returns a boolean value. Return true if the element exists in the map. Otherwise, return false. You can use the has() method as follows:

map.has(key)

Code Example

In this example, We use the has() method to check if the object’s key 'bestPlayer' exists on the map.

Let’s see the code example below.

const bestPlayer = {
    name: "Cristiano Ronaldo",
    age: 37,
    nationality: "Portugal",
    salary: "26,8 million GBP",
};

const map = new Map([["nationality", "Portugal"]]);

// Using the map.has() method to check if the key nationality contain in Map
console.log(
    "Check Map has object key",
    Object.keys(bestPlayer)[2],
    map.has(Object.keys(bestPlayer)[2])
);

// Using the map.has() method to check if the key salary contains in Map
console.log(
    "Check Map has object key",
    Object.keys(bestPlayer)[3],
    map.has(Object.keys(bestPlayer)[3])
);

Output

Check Map has object key nationality true
Check Map has object key salary false

Create the function to check

In a second way, we create the function ‘checkKey‘ to check if a map has an object key by using the for loop method. In this way, we loop to all the keys of the map and check with the object’s key by using strict equality (===) in JavaScript. If the key exists on a map, the hasKey value will be true. If not, it will be set to false.

Example

Let’s see the example below to get more information.

const bestPlayer = {
    name: "Cristiano Ronaldo",
    age: 37,
    nationality: "Portugal",
    salary: "26,8 million GBP",
};

const map = new Map([["nationality", "Portugal"]]);

// Create the function to check the object key exits or not in map
function checkKey(k) {
    for (const key of map) {
        if (k === key) {
            var hasKey = true;
        } else {
            hasKey = false;
        }
    }
    console.log("Check map has object key", k, hasKey);
}

checkKey(Object.keys(bestPlayer)[2]);
checkKey(Object.keys(bestPlayer)[3]);

Output

Check map has object key nationality true 
Check map has object key salary false

Summary

In this article, we already introduced two methods to check if a map has an object key in JavaScript, like using the has() method and creating the function with for loop. But the has() method is very simple, easy, and fast to solve this problem. Leave your comment here if you have any questions about this article.

Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *