JS: Map Substract 🚀

By Xah Lee. Date: . Last updated: .

Substract Map

/* substract first map by second.
return a new map.
version 2024-04-19
*/
const xah_map_substract = (Amap, Bmap) => {
  const xresult = new Map();
  Amap.forEach((vv, kk) => {
    if (Bmap.has(kk) && (Bmap.get(kk) === vv)) {
    } else {
      xresult.set(kk, vv);
    }
  });
  return xresult;
};

const xxt = new Map([[3, "n3"], [4, "n4"], [5, "n4"]]);
const xxh = new Map([[3, "n3"], [4, "n4"]]);

console.log(xah_map_substract(xxt, xxh));
// Map(1) { 5 => "n4" }

JavaScript, Map Object