To check if two maps are equal in JavaScript, you need to compare their sizes and iterate over the key-value pairs to ensure they match. This method provides a straightforward way to determine if two maps contain the same elements.
We can check if two maps are equal in JavaScript by comparing their sizes and iterating over the key-value pairs to ensure they match.
For example,
map1
and map2
with some key-value pairs. In this example, the maps have string keys and integer values.map1
and map2
are equal. If their sizes are not equal, the maps are not equal.map1
and check if each key-value pair exists in map2
and matches the corresponding value.console.log
function to indicate whether the maps are equal or not.const map1 = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
const map2 = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
// Check if the maps are equal
let equal = true;
if (map1.size !== map2.size) {
equal = false;
} else {
for (let [key, value] of map1) {
if (!map2.has(key) || map2.get(key) !== value) {
equal = false;
break;
}
}
}
if (equal) {
console.log('Maps are equal');
} else {
console.log('Maps are not equal');
}
Maps are equal
We can also check if two maps with different sizes are equal in JavaScript by comparing their sizes first and then iterating over the key-value pairs to ensure they match.
For example,
map1
and map2
with some key-value pairs. In this example, the maps have string keys and integer values, and they have different sizes.map1
and map2
are equal. If their sizes are not equal, the maps are not equal.map1
and check if each key-value pair exists in map2
and matches the corresponding value.console.log
function to indicate whether the maps are equal or not.const map1 = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
const map2 = new Map([
['one', 1],
['two', 2]
]);
// Check if the maps are equal
let equal = true;
if (map1.size !== map2.size) {
equal = false;
} else {
for (let [key, value] of map1) {
if (!map2.has(key) || map2.get(key) !== value) {
equal = false;
break;
}
}
}
if (equal) {
console.log('Maps are equal');
} else {
console.log('Maps are not equal');
}
Maps are not equal
We can create a helper function to check if two maps are equal in JavaScript, which provides a reusable way to compare maps.
For example,
map1
and map2
with some key-value pairs. In this example, the maps have string keys and integer values.areMapsEqual
that takes two maps as parameters and returns a boolean value indicating whether the maps are equal.false
.map1
and check if each key-value pair exists in map2
and matches the corresponding value, returning true
if they do.map1
and map2
as arguments and print the result to the console using the console.log
function to indicate whether the maps are equal or not.const map1 = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
const map2 = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
// Helper function to check if two maps are equal
function areMapsEqual(map1, map2) {
if (map1.size !== map2.size) {
return false;
}
for (let [key, value] of map1) {
if (!map2.has(key) || map2.get(key) !== value) {
return false;
}
}
return true;
}
// Check if the maps are equal using the helper function
if (areMapsEqual(map1, map2)) {
console.log('Maps are equal');
} else {
console.log('Maps are not equal');
}
Maps are equal
In this tutorial, we learned How to Check if Two Maps are Equal in JavaScript language with well detailed examples.