JS: Map Equality 🚀

By Xah Lee. Date: .

Test Map Equality

/*
xah_map_equal(Amap, Bmap)
return true if two maps have the same keys and values.
Keys are compared by triple equal.
Values are compared by triple equal.

JS: Map Equality 🚀
http://xahlee.info/js/js_map_equality.html
version 2024-04-20
*/

const xah_map_equal = (Amap, Bmap) => {
  if (Amap.size !== Bmap.size) return false;

  Amap.forEach((vv, kk) => {
    if (!Bmap.has(kk)) return false;
    if (Bmap.get(kk) !== vv) return false;
  });

  return true;
};

// HHHH---------------------------------------------------

const xx = new Map([[4, "n4"], [3, "n3"]]);
const yy = new Map([[3, "n3"], [4, "n4"]]);

console.log(xah_map_equal(xx, yy));

JavaScript, Map Object