JavaScript Map Methods
The Map
object in JavaScript allows you to store key-value pairs where keys can be of any data type. The Map
methods provide various functionalities to manipulate and retrieve data from a map. This article covers some of the essential methods available for the Map
object.
Map Methods
Here are some commonly used methods of the Map
object:
// Create a new Map
const map = new Map();
// Adding entries
map.set('name', 'Alice');
map.set('age', 30);
map.set('city', 'New York');
// Displaying all entries using a loop
console.log('Initial Map Entries:');
map.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Method: get
console.log('\nGet Method:');
console.log(`Name: ${map.get('name')}`); // Output: Alice
// Method: has
console.log('\nHas Method:');
console.log(`Has 'age' key: ${map.has('age')}`); // Output: true
// Method: delete
console.log('\nDelete Method:');
map.delete('city');
console.log(`Has 'city' key after deletion: ${map.has('city')}`); // Output: false
// Method: size
console.log('\nSize Method:');
console.log(`Size of map: ${map.size}`); // Output: 2
// Adding more entries
map.set('country', 'USA');
map.set('occupation', 'Engineer');
// Displaying all entries again
console.log('\nUpdated Map Entries:');
map.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Method: entries
console.log('\nEntries Method:');
for (const [key, value] of map.entries()) {
console.log(`${key}: ${value}`);
}
// Method: keys
console.log('\nKeys Method:');
for (const key of map.keys()) {
console.log(key);
}
// Method: values
console.log('\nValues Method:');
for (const value of map.values()) {
console.log(value);
}
// Method: clear
console.log('\nClear Method:');
map.clear();
console.log(`Size of map after clear: ${map.size}`); // Output: 0
Map Methods Explained
01. set
The set
method adds a new key-value pair to the map. If the key already exists, the method updates the value associated with the key.
const map = new Map();
map.set('key1', 'value1');
console.log(map.get('key1')); // Output: value1
02. get
The get
method retrieves the value associated with a specified key. If the key does not exist, it returns undefined
.
const map = new Map();
map.set('key1', 'value1');
console.log(map.get('key1')); // Output: value1
console.log(map.get('nonExistentKey')); // Output: undefined
03. has
The has
method checks if a specified key exists in the map. It returns true
if the key is present, otherwise false
.
const map = new Map();
map.set('key1', 'value1');
console.log(map.has('key1')); // Output: true
console.log(map.has('nonExistentKey')); // Output: false
04. delete
The delete
method removes a key-value pair from the map based on the specified key. It returns true
if the key was successfully removed, otherwise false
.
const map = new Map();
map.set('key1', 'value1');
map.delete('key1');
console.log(map.has('key1')); // Output: false
05. clear
The clear
method removes all key-value pairs from the map, leaving it empty.
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
map.clear();
console.log(map.size); // Output: 0
06. keys
The keys
method returns an iterator object that contains the keys of the map in insertion order.
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
for (let key of map.keys()) {
console.log(key);
}
// Output:
// key1
// key2
07. values
The values
method returns an iterator object that contains the values of the map in insertion order.
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
for (let value of map.values()) {
console.log(value);
}
// Output:
// value1
// value2
08. entries
The entries
method returns an iterator object that contains an array of [key, value] pairs in insertion order.
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
for (let [key, value] of map.entries()) {
console.log(`${key}: ${value}`);
}
// Output:
// key1: value1
// key2: value2
09. forEach
The forEach
method executes a provided function once for each key-value pair in the map.
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
map.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Output:
// key1: value1
// key2: value2
Conclusion
JavaScript's Map
object provides a robust and versatile way to manage key-value pairs. By utilizing the various methods available, you can efficiently manipulate, access, and iterate over map entries. Understanding these methods will help you make the most of the Map
object in your JavaScript applications, leading to more effective and organized data management.
Comments
Post a Comment