JS: Test Equality of Map Objects 📜

By Xah Lee. Date: . Last updated: .

Test Map Equality

/*
xah_map_equal(xmapA, xmapB)
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 = (xmapA, xmapB) => {
  if (xmapA.size !== xmapB.size) return false;

  xmapA.forEach((vv, kk) => {
    if (!xmapB.has(kk)) return false;
    if (xmapB.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));
// true

JavaScript. Map Object