const user = new Map([
['First Name', 'Divine'],
['Last Name', 'Orji'],
['Age', 24],
[false, 'Dance'],
]);
// PROPERTIES
// Map.prototype.size: This returns the number of items in a Map.
console.log(user.size)
// 4
// METHODS
// Here are some methods you can use with Map:
// 1. Map.prototype.set(key, value): Adds a new key: value entry to your Map.
user.set('Pet', 'Gary');
console.log(user);
/*
Map(5) {
'First Name' => 'Divine',
'Last Name' => 'Orji',
'Age' => 24,
false => 'Dance',
'Pet' => 'Gary'
}
*/
// 2. Map.prototype.has(key): Checks if the specified key exists in your Map.
console.log(user.has('Age'));
// true
// 3. Map.prototype.get(key): Gets the value associated with the specified key,
// or returns undefined if the key does not exist.
console.log(user.get('Pet'));
// Gary
console.log(user.get('House'));
// undefined
// 4. Map.prototype.delete(key): Deletes the specified key and its value,
// then returns true. If the key does not exist, it returns false.
console.log(user.delete('Age'));
// true
console.log(user);
/*
Map(4) {
'First Name' => 'Divine',
'Last Name' => 'Orji',
false => 'Dance',
'Pet' => 'Gary'
}
*/
console.log(user.delete('House'));
// false
// 5. Map.prototype.entries(): Returns an object with each key: value pair stored as an array.
console.log(user.entries());
/*
[Map Entries] {
[ 'First Name', 'Divine' ],
[ 'Last Name', 'Orji' ],
[ false, 'Dance' ],
[ 'Pet', 'Gary' ]
}
*/
// 6. Map.prototype.keys(): Returns an object with all the keys in your Map.
console.log(user.keys());
// [Map Iterator] { 'First Name', 'Last Name', false, 'Pet' }
// 7. Map.prototype.values(): Returns an object with all the values in your Map.
console.log(user.values());
// [Map Iterator] { 'Divine', 'Orji', 'Dance', 'Gary' }
// 8. Map.prototype.forEach(): Executes a callback function on each entry in your Map.
user.forEach((value) => {
console.log(value);
});
/*
Divine
Orji
Dance
Gary
*/
// 9. Map.prototype.clear(): Deletes all entries in your Map.
user.clear()
console.log(user)
// Map(0) {}