Check if a value exists in a map in JavaScript
This post will discuss how to check if a value exists in a map in JavaScript. In other words, determine whether there is a key in the map that has the given value as its corresponding value.
The Map class provides the has() function that checks if the specified key exists in the map or not. However, if we need to check if a value exists in a map in JavaScript, we can use any of the following functions:
1. Using Array.includes() function
If the map is a Map object that stores key-value pairs, we can use the Map.values() function with the spread operator (…) to get an array of the map’s values, and then use the Array.includes() function to determine whether the array includes a certain value among its entries, returning true or false as appropriate. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 |
// Create a map of names and ages let map = new Map([["Henry", 25], ["Liam", 30], ["Matthew", 35]]); // Get array of values of the map let values = [...map.values()]; // Check if the array includes the values console.log(values.includes(25)); // true console.log(values.includes(40)); // false |
If the map is an object literal that stores key-value pairs, we can use the Object.values() function to get an array of the object’s values, and then invoke the Array.includes() function to check if the value is contained in the array. For example:
|
1 2 3 4 5 6 7 8 9 |
// Create an object of names and ages const myObj = {"Henry": 25, "Liam": 30, "Matthew": 35}; // Get array of values of the object let values = Object.values(myObj); // Check if the array includes the values console.log(values.includes(25)); // true console.log(values.includes(40)); // false |
2. Using Set.has() function
Another option is to use the Set object to store the map’s values as a collection of unique values, and then use the Set.has() function to check if the value is present in the set. For example:
|
1 2 3 4 5 6 7 8 9 |
// Create a map of names and ages let map = new Map([["Henry", 25], ["Liam", 30], ["Matthew", 35]]); // Create a set from values of the map let values = new Set(map.values()); // Check if the set has the values console.log(values.has(25)); // true console.log(values.has(40)); // false |
3. Using a cutom function
We can also create a cutom function to check if the value we are looking for is included in the values of the map. This function iterates over the key-value pairs in the map with a for…of loop and compares each value with the value we are looking for. If a match is found, it returns true. If no match is found, it returns false after completing the loop. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Define a function to check if a value exists in a map function hasValue(map, value) { // Iterate over the key-value pairs in the map for (let [key, val] of map) { // If the current value matches the given value, return true if (val === value) { return true; } } return false; } // Create a map of names and ages let map = new Map([["Henry", 25], ["Liam", 30], ["Matthew", 35]]); // Check if the array includes the values console.log(hasValue(map, 25)); // true console.log(hasValue(map, 40)); // false |
That’s all about checking if a value exists in a map in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)