JavaScript: Map Filter

By Xah Lee. Date: . Last updated: .

Here's a function that filters map object.

/* [
Returns 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;
});

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

JavaScript Map Object

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc