Welcome back, future JavaScript master! Our mission is to master JavaScript Maps, a tool for managing data using key-value pairs that distinguish them from objects (dictionaries). Buckle up; we're about to dive deep into data management using maps in JavaScript!
A Map in JavaScript is a guide to the treasures of unique key-value pairs. Unlike JavaScript objects, which mostly use strings as keys, Map
permits any type of keys, thus charting a unique path to each treasure.
Here is an example of creating and populating a Map:
JavaScript1const spacecrafts = new Map(); // Build a new Map 2spacecrafts.set(1234, 'Numerical Planet'); // Using numerical key 3spacecrafts.set(true, 'Boolean Planet'); // Using boolean key 4spacecrafts.set('Star Destroyer', 'Death Star'); // Using common string key 5 6console.log(spacecrafts); 7/* 8Prints: 9Map(3) {1234: "Numerical Planet", true: "Boolean Planet", "Star Destroyer": "Death Star"} 10*/
You can see how Map
supports different types of keys without any issues.
Maps have built-in methods such as get
, set
, delete
, and has
that enable efficient data management. The get
method retrieves the value of a given key, has
verifies the existence of a key in the Map, and delete
eliminates a key-value pair:
JavaScript1console.log(spacecrafts.get(1234)); // Output: Numerical Planet 2console.log(spacecrafts.has('Star Destroyer')); // Output: true 3spacecrafts.delete('Star Destroyer'); // Remove 'Star Destroyer' from our Map 4console.log(spacecrafts.has('Star Destroyer')); // Output: false
The get
method returns undefined
for absent keys. However, you can use a conditional operator to provide a default value in these situations:
JavaScript1let spacecraft = 'Voyager'; 2// If 'Voyager' exists on our Map, get its destination. Otherwise, print 'Unknown destination' 3console.log(spacecrafts.has(spacecraft) ? spacecrafts.get(spacecraft) : 'Unknown destination'); // Output: Unknown destination
While objects in JavaScript predominantly hold string keys, Maps can accommodate any type of keys and preserve the order of insertion, making them ideal for complex data manipulation.
Here is how you can use more advanced types of keys in the Map
structure, specifically, the key of an object data type:
JavaScript1let planetMap = new Map(); // Create a Map 2planetMap.set({name: 'Mars'}, 'Fourth planet from the Sun'); // Add key-value pair 3console.log(planetMap.get({name: 'Mars'})); // Try to get value - Output: undefined 4// undefined is returned, as `{name: 'Mars'}` is constructed once again, so it's a different object 5 6let planet = {name: 'Mars'}; // Define an object 7planetMap.set(planet, 'Fourth planet from the Sun'); // Set this object as a key in our Map 8console.log(planetMap.get(planet)); // Get the value for this object key - Output: Fourth planet from the Sun
In this context, the object {name: 'Mars'}
serves as a unique key that possesses its values.
Congratulations! You've mastered JavaScript Maps, a powerful data management structure. Now, solidify your understanding of Maps through hands-on exercises. Remember, practice makes perfect as we delve deeper into the cosmos of JavaScript!