How to filter a JavaScript Map?

 

To use a .filter() iterator for a map, we can use a simple trick because there is no .filter operator for ES6 Maps

 

const map0 = new Map([
  ['a', 1],
  ['b', 2],
  ['c', 3]
]);

const map1 = new Map(
  [...map0]
  .filter(([k, v]) => v < 3 )
);

console.info([...map1]); 
//[0: ["a", 1], 1: ["b", 2]]

Tags:

Share:

Related posts