Last updated: Mar 4, 2024
Reading timeยท3 min
To filter a Map
in JavaScript:
Map.forEach()
method to iterate over the Map
.Map.delete()
method to delete all Map
elements that don't meet
the condition.const map1 = new Map([ ['num1', 1], ['num2', 2], ['num3', 3], ]); map1.forEach((value, key) => { if (value > 1) { map1.delete(key); } }); // ๐๏ธ {'num1' => 1} console.log(map1);
We used the Map.forEach() method to iterate over the Map object.
The function we passed to the forEach()
method gets called for each key-value
pair of the Map
and gets passed the following parameters:
value
- the value of the iterationkey
- the key of the iterationmap
- the Map
object that's being iteratedWe make use of the value
to check for a condition and of the key
to
delete
the Map
elements that don't meet the condition.
An alternative approach is to convert the Map
object to an array and use the
filter
method.
filter()
This is a four-step process:
Map
to an array.filter()
method to iterate over the array.Map
.const map1 = new Map([ ['num1', 1], ['num2', 2], ['num3', 3], ]); const filtered = new Map( Array.from(map1).filter(([_key, value]) => { if (value > 1) { return true; } return false; }), ); // ๐๏ธ {'num2' => 2, 'num3' => 3} console.log(filtered);
We used the Array.from()
method to convert the Map
to an array of key-value
pairs.
const map1 = new Map([ ['num1', 1], ['num2', 2], ['num3', 3], ]); // ๐๏ธ [['num1', 1], ['num2', 2], ['num3', 3]] console.log(Array.from(map1));
When we convert the Map
to an array, we get back a two-dimensional array, on
which we can call the Array.filter()
method.
The first element in each nested array is the key and the second is the corresponding value.
We used destructuring assignment to
destructure the key and value in the callback function we passed to the
filter()
method.
const [key, value] = ['num1', 1]; console.log(key); // ๐๏ธ num1 console.log(value); // ๐๏ธ 1
In the callback function, we check for a condition and return true
if the
condition is met and false
otherwise.
The filter()
method returns a new array that only contains the elements that
meet the condition.
You can also shorten the callback function by returning the condition directly.
const map1 = new Map([ ['num1', 1], ['num2', 2], ['num3', 3], ]); const filtered = new Map( Array.from(map1).filter(([_key, value]) => { return value > 1; }), ); // ๐๏ธ {'num2' => 2, 'num3' => 3} console.log(filtered);
Instead of returning true
or false
explicitly, the code sample returns the
condition directly.
The last step is to convert the array back to a Map
using the
Map() constructor.
for...of
You can also filter a Map
by using a for...of
loop.
const map1 = new Map([ ['num1', 1], ['num2', 2], ['num3', 3], ['num3', 4], ]); for (const [key, value] of map1) { if (value > 1) { map1.delete(key); } } // ๐๏ธ {'num1' => 1} console.log(map1);
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we check if the current value meets a certain condition and
delete the elements we don't want to keep in the Map
.
You can learn more about the related topics by checking out the following tutorials: