Francesco Borzi
Francesco Borzi

Reputation: 61894

How to check if two Maps have the same key set in JavaScript

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

Answers (7)

Keith Whittingham
Keith Whittingham

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

Koushik Chatterjee
Koushik Chatterjee

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

Nina Scholz
Nina Scholz

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

CertainPerformance
CertainPerformance

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

amrender singh
amrender singh

Reputation: 8239

Basically you need to check for two things:

  1. Size of both the maps, if they are unequal than simply return false.
  2. If size is same than check if all the keys of map1 are present in map2,if they are than return true else return false.

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

Dez
Dez

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

Fabian N.
Fabian N.

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

Related Questions