Conditionally remove keys from a map in JavaScript
This post will discuss how to remove all keys from a map that matches a specific condition in JavaScript.
There are several ways to conditionally remove keys from a map in JavaScript. A map is a data structure that stores key-value pairs, where each key is unique and can be any type of value. Here are some of the methods we can use:
1. Using Array.filter() function
The spread syntax (…) spreads the elements of a map into an array of [key, value] pairs, and then uses the Array.filter() function to create a new array with only the pairs that pass a test function. Then, we can use the Map constructor to convert the filtered array back to a map. This function does not modify the original map, but creates a new one. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Create a map of names and ages let map = new Map([['Henry', 15], ['Matthew', 10], ['Christopher', 25]]); // Define a test function function isAdult([key, value]) { return value >= 18; } // Spread the map into an array and filter it by the test function let filteredArray = [...map].filter(isAdult); // Convert the filtered array back to a map let filteredMap = new Map(filteredArray); // The filtered map contains only the pairs that pass the test function console.log(filteredMap); // Map(1) { 'Christopher' => 25 } |
2. Using Map.delete() function
The Map.delete() function removes an element with the specified key from the map and returns a boolean indicating whether the deletion was successful or not. It returns true if the property is successfully deleted, and false otherwise. We can use this function if we know the specific key that we want to remove. For example, we can use the following code snippet to remove the key "Henry" from a map:
|
1 2 3 4 5 6 7 8 9 |
// Create a map of names and ages let map = new Map([['Henry', 15], ['Matthew', 10], ['Christopher', 25]]); // Remove the key 'Henry' from the map let result = map.delete('Henry'); console.log(result); // true // Check if the map still has the key 'Henry' console.log(map.has('Henry')); // false |
3. Using a loop
This function iterates over the key-value pairs in the map with a for…of loop and deletes the pairs that match a certain condition. We can use this function if we want to remove multiple keys based on some logic. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Create a map of names and ages let map = new Map([['Henry', 15], ['Matthew', 10], ['Christopher', 25]]); // Define a condition function function isTeen(value, key, map) { return value < 18; } // Iterate over the map and delete the pairs that meet the condition for (let [key, value] of map) { if (isTeen(value, key, map)) { map.delete(key); } } // The map now contains only the pairs that do not meet the condition console.log(map); // Map(1) { 'Christopher' => 25 } |
That’s all about conditionally removing keys from a map 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 :)