How to filter a map in JavaScript

1 Answer

0 votes
const myMap = new Map([
  ['a', 1],
  ['b', 2],
  ['c', 3],
  ['d', 4],
  ['e', 5]
]);

// Filter for values greater than 2
const filteredMap = new Map(
    [...myMap.entries()].filter(([key, value]) => value > 2)
);

console.log(filteredMap); 



/*
run:

Map(3) { 'c' => 3, 'd' => 4, 'e' => 5 }

*/

 



answered Aug 7, 2025 by avibootz

Related questions

...