Remove entries with null values from a Map in JavaScript
This post will discuss how to remove entries with null values from a Map in JavaScript.
To remove entries with null values from a map in JavaScript, we need to iterate through the map entries and delete the entries that have a null value. We can use different functions to do this, such as:
1. Using Map.forEach() function
The Map.forEach() function executes a callback function for each entry in the map. We can use this function to check the value of each entry and delete it if it is null. For example, we can use the following code snippet to remove entries with null values from a map:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create a map let map = new Map ([['a', 1], ['b', null], ['c', 5], ['d', null], ['e', 3]]); // Loop through the map entries map.forEach ((value, key) => { // Check if the value is null if (value === null) { // Delete the entry from the map map.delete(key); } }); // Map(3) { 'a' => 1, 'c' => 5, 'e' => 3 } console.log(map); |
Alternatively, we can use the for…of loop to access the key and value of each entry in the map and delete it if the value is null. For example, we can use the following code snippet to remove entries with null values from a map:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create a map let map = new Map ([['a', 1], ['b', null], ['c', 5], ['d', null], ['e', 3]]); // Loop through the map entries for (let [key, value] of map) { // Check if the value is null if (value === null) { // Delete the entry from the map map.delete(key); } } // Map(3) { 'a' => 1, 'c' => 5, 'e' => 3 } console.log(map); |
This is the simplest and most straightforward way, but it requires us to mutate the original map and use the delete operator. However, it does not work to create a new map without null values.
2. Using Array.filter() function
We can use the Array.from() function to convert the map into an array of entries and use the Array.filter() function to remove the ones with null values. 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. This approach is preferred to create a new map without null values, and prefer a more declarative and expressive style of coding. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create a map let map = new Map ([['a', 1], ['b', null], ['c', 5], ['d', null], ['e', 3]]); // Convert the map into an array of entries const entries = Array.from(map); // Filter the entries by value const filtered = entries.filter(([key, value]) => value !== null); // Create a new map from the filtered array const newMap = new Map(filtered); // Map(3) { 'a' => 1, 'c' => 5, 'e' => 3 } console.log(newMap); |
3. Using a custom function
Use a custom function that takes a map as a parameter and returns a new map without null values. This is a more reusable and modular way, but it requires us to write and maintain our own function. For instance:
|
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 |
// Create a custom function function removeNullValues(map) { // Initialize an empty array const filtered = []; // Loop through the map entries for (let [key, value] of map) { // Check if the value is not null if (value !== null) { // 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); } // Create a map let map = new Map ([['a', 1], ['b', null], ['c', 5], ['d', null], ['e', 3]]); // Call the function with the map const newMap = removeNullValues(map); // Map(3) { 'a' => 1, 'c' => 5, 'e' => 3 } console.log(newMap); |
This method works well if we need more control over how the values are compared or to reuse the function for different maps. However, it does not work to modify the original map or to use a more concise or functional style of coding.
That’s all about removing entries with null values 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 :)