JavaScript Maps are a built-in data structure in JavaScript that allows you to store and retrieve key-value pairs. Maps are similar to JavaScript objects, but they provide more features, such as the ability to use any value as a key (not just strings), iterate over keys and values in the order they were added, and easily get the size of the map. Maps also have methods for adding, deleting, and updating key-value pairs. Overall, Maps are a useful tool for managing complex data structures and are widely used in JavaScript programming.
new Map()
The new Map() method creates a new Map object. It can be called with or without the 'new' keyword.
Syntax:
let mapName = new Map();
Example:
let myMap = new Map();
set()
The set() method sets the value for a key in a Map. If the key already exists, the old value is replaced.
Syntax:
myMap.set(key, value);
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
Output:
Map(2) { 'name' => 'John', 'age' => 30 }
get()
The get() method gets the value for a key in a Map. If the key doesn't exist, it returns undefined.
Syntax:
myMap.get(key);
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
console.log(myMap.get("name"));
Output:
John
delete()
The delete() method removes a Map element specified by the key.
Syntax:
myMap.delete(key);
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
myMap.delete("age");
Output:
Map(1) { 'name' => 'John' }
has()
The has() method returns true if a key exists in a Map, otherwise false.
Syntax:
myMap.has(key);
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
console.log(myMap.has("name"));
Output:
true
forEach()
The forEach() method calls a function for each key/value pair in a Map.
Syntax:
myMap.forEach(callbackFunction);
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
myMap.forEach((value, key) => {
console.log(key + " = " + value);
});
Output:
name = John
age = 30
entries()
The entries() method returns an iterator with the [key, value] pairs in a Map.
Syntax:
myMap.entries();
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
console.log([...myMap.entries()]);
Output:
[ [ 'name', 'John' ], [ 'age', 30 ] ]
size
The size property returns the number of elements in a Map.
Syntax:
myMap.size;
Example:
let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);
console.log(myMap.size);
Output:
2
Summarizing Up
In JavaScript, a map is a collection of key-value pairs, where each key can only appear once. It is similar to an object, but with a few key differences. Maps provide an easy way to store and retrieve data based on unique keys. The keys in a map can be of any type, including objects and functions.
Maps can be created using the built-in Map constructor function or using the map literal notation. The Map constructor takes an optional iterable object (such as an array) containing key-value pairs to initialize the map.
To add a key-value pair to a map, use the set() method. To retrieve the value for a specific key, use the get() method. The has() method can be used to check if a key exists in the map, and the delete() method can be used to remove a key-value pair.
Maps also have a size property that returns the number of key-value pairs in the map. Additionally, the keys(), values(), and entries() methods can be used to get an iterable of the keys, values, and key-value pairs in the map, respectively.
In summary, JavaScript maps are a useful data structure for storing and retrieving key-value pairs. They offer flexibility in the types of keys they can accept and provide a range of methods for working with the data they store.