Reputation: 61894
Suppose to have two Map objects, how to check if their keys sets are the same?
For example:
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
in this case both A
and B
maps have the same key set (which is ['x', 'y']
), while the key set of C
is different since it has the extra key z
.
Upvotes: 0
Views: 2302
Reputation: 326
Here's a one liner wrapped in a TypeScript'ized function
function sameKeys(a: Map<string, string>, b: Map<string, string>): boolean {
return a.size === b.size && [...a.keys()].every(key => b.has(key))
}
Upvotes: 0
Reputation: 4175
You can construct a new Map
with both of their entries, and then compare size. Anyway you need to check the size of both of them and if its same then only you should proceed for this.
map1.size.size
=== map2.size &&
new Map([...map1, ...map2])).size
=== map1.size
//or map2.size
Let's create a working example:
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
let compareMap = (m1, m2) => (
m1.size === m2.size &&
(new Map([...m1, ...m2])).size === m1.size
)
console.log('Compare A & B: ', compareMap(A, B));
console.log('Compare A & C: ', compareMap(A, C));
console.log('Compare B & C: ', compareMap(B, C));
Upvotes: 0
Reputation: 386654
You could check the size and take the prototype of has
and the second map as thisArg
for checking all keys with Array#some
.
This works for any types, because it does not mutate the type of the keys.
const
compare = (a, b) => a.size === b.size && [...a.keys()].some(Map.prototype.has, b),
a = new Map([['x', 123], ['y', 345]]);
b = new Map([['y', 567], ['x', 789]]);
c = new Map([['x', 121], ['y', 232], ['z', 434]]);
console.log(compare(a, b));
console.log(compare(a, c));
Upvotes: 1
Reputation: 370879
Check that each map's size
is the same, and then iterate over the keys
of one Map
and check that the key exists in the other as well. Utilizing Array.prototype.every.call
means that there's no need to create an intermediate array:
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
const sameKeySet = (m1, m2) => (
m1.size === m2.size
&& Array.prototype.every.call(m1.keys(), key => m2.has(key))
);
console.log(sameKeySet(A, B));
console.log(sameKeySet(A, C));
Upvotes: 5
Reputation: 8239
Basically you need to check for two things:
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
const D = new Map();
C.set('x', 121);
C.set('z', 232);
function isSame(a,b){
if(a.size != b.size)
return false;
for(const [key, value] of a.entries()){
if(!b.has(key))
return false;
}
return true;
}
console.log(isSame(A,B));
console.log(isSame(A,C));
console.log(isSame(A,D));
Upvotes: 1
Reputation: 5838
You can convert the keys of a Map into an array by spreading the iterator returned by the keys()
method:
const aKeys = [...A.keys()];
Then you just will have to compare all the keys arrays. For the case you show up, you can simple do:
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
const aKeys = [...A.keys()];
const bKeys = [...B.keys()];
const cKeys = [...C.keys()];
console.log(aKeys.sort().toString() == bKeys.sort().toString());
console.log(aKeys.sort().toString() == cKeys.sort().toString());
console.log(bKeys.sort().toString() == cKeys.sort().toString());
Upvotes: 1
Reputation: 3856
You could check the size and then iterate over the keys of one map and check that the other one has them too.
const A = new Map();
A.set('x', 123);
A.set('y', 345);
const B = new Map();
B.set('y', 567);
B.set('x', 789);
const C = new Map();
C.set('x', 121);
C.set('y', 232);
C.set('z', 434);
function sameKeys(a, b) {
if (a.size != b.size) {
return false;
}
for (let key in a.keys()) {
if (!b.has(key)) {
return false;
}
}
return true;
}
console.log(sameKeys(A, B));
console.log(sameKeys(A, C));
Upvotes: 1