Creating a Map
To create a new Map
object, you can simply use the new
keyword and call the Map
constructor:
const myMap = new Map();
Adding and Accessing Values
To add a key-value pair to a Map
, you can use the set()
method:
myMap.set("name", "John");
myMap.set("age", 30);
To access a value based on a key, you can use the get()
method:
const name = myMap.get("name");
console.log(name); // Output: "John"
You can also check if a key exists using the has()
method:
const hasAge = myMap.has("age");
console.log(hasAge); // Output: true
Removing Values
To remove a specific key-value pair from a Map
, you can use the delete()
method:
myMap.delete("name");
You can also clear all key-value pairs in a Map
using the clear()
method:
myMap.clear();
Iterating over a Map
There are several ways to iterate over the key-value pairs in a Map
.
- Using the
for...of
loop:
for (const [key, value] of myMap) {
console.log(`${key}: ${value}`);
}
- Using the
forEach()
method:
myMap.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
Map Size
To get the number of key-value pairs in a Map
, you can use the size
property:
console.log(myMap.size); // Output: 1
Conclusion
The Map
object in JavaScript provides a convenient way to store and manipulate key-value pairs. It offers various methods for adding, accessing, and removing values, as well as iterating over the entries. Understanding the basic syntax and usage of the Map
object is essential for working with complex data structures in JavaScript.
[hashtags: #JavaScript #MapObject]