Remove entries from a map while iterating over it in JavaScript
This post will discuss how to remove entries from a map while iterating over it in JavaScript.
To remove entries from a map while iterating it in JavaScript, we need to use the Map.delete() function to delete the entries that meet a certain condition. We can use different functions to iterate over the map, such as:
1. Using Map.delete() function
We can use the Map.delete() function with the Map.forEach() function to remove entries from a map. The forEach() function executes a callback function for each key-value pair in the map, which can check if the current key-value pair meet some condition for removal. If they do, it can use the delete() function to remove the pair from the map. For example, we can use the following code snippet to remove entries with odd values from a map:
|
1 2 3 4 5 6 7 8 9 10 |
const myMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]]); myMap.forEach((value, key) => { // remove entries with odd values if (value % 2 !== 0) { myMap.delete(key); } }); console.log(myMap); // Map(2) { 'b' => 2, 'd' => 4 } |
Alternatively, we can use the for…of loop to iterate over the map entries and delete the ones that meet a condition. For example, we can use the following code snippet to remove entries with specific key or value from a map:
|
1 2 3 4 5 6 7 8 9 10 11 |
const myMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]]); for (let [key, value] of myMap) { // remove entries with specific key or value if (key === 'b' || value === 4) { myMap.delete(key); } } console.log(myMap); // Map(2) { 'a' => 1, 'c' => 3 } |
2. Using Array.filter() function
The Array.from() function to convert the map into an array of key-value pairs. The Array.filter() function creates a new array with all elements that pass a test implemented by a callback function. By combining these two functions, we can convert the map to an array and use the filter() function to filter out the key-value pairs that meet some condition for removal. Then use the new Map() constructor to create a new map from the filtered array. This is a more functional and concise way, but it requires us to create two intermediate arrays and use arrow functions. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
const myMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]]); // Convert the map into an array of entries const entries = Array.from(myMap); // Filter the entries by value const filtered = entries.filter(([key, value]) => value % 2 !== 0); // Create a new map from the filtered array const filteredMap = new Map(filtered); console.log(filteredMap); // Map(2) { 'a' => 1, 'c' => 3 } |
3. Using a custom function
Use a custom function that takes a map and a condition as parameters and returns a new map without the deleted entries. This is a more reusable and modular way, but it requires us to write and maintain our own function. For example, the following code create a custom function and removes entries with even values using it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Create a custom function function removeEntriesByCondition(map, condition) { // Initialize an empty array const filtered = []; // Loop through the map entries for (let [key, value] of map) { // Check if the value does not meet the condition if (!condition(value)) { // Push the entry into the array filtered.push([key, value]); } } // Create a new map from the filtered array and return it return new Map(filtered); } const myMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]]); // Define a condition function function isEven(value) { return value % 2 === 0; } // Call the function with the map and the condition const newMap = removeEntriesByCondition(myMap, isEven); console.log(newMap); // Map(2) { 'a' => 1, 'c' => 3 } |
That’s all about removing entries from a map while iterating over it in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)