JS: Map Filter 🚀

By Xah Lee. Date: . Last updated: .

Here is a function that filters map object.

/*
Return a new map where f(key,value) returns true.
http://xahlee.info/js/js_map_filter.html
Version 2018-04-13
*/
const xah_map_filter = ((map_obj, f) => {
  const result = new Map();
  for (let [k, v] of map_obj) if (f(k, v)) result.set(k, v);
  return result;
});

// ssss---------------------------------------------------
// test
const xx = new Map([[3, "n3"], [4, "n4"]]);
console.log(xah_map_filter(xx, (k, v) => ((v === "n4") ? true : false)));
// Map { 4 => "n4" }

JavaScript, Map Object