In this tutorial, we will explore the Map object in JavaScript. The Map is a built-in data structure that allows you to store key-value pairs. Unlike objects, keys in a Map can be of any type, including functions and other objects. This makes Maps more flexible and powerful for certain use cases.
The Map object holds key-value pairs where each key is unique. A key in a Map can be of any type, not just strings or symbols like in regular JavaScript objects. This flexibility makes Maps particularly useful when you need to associate arbitrary values with keys that are not limited to primitive types.
You can create a new Map using the new Map() constructor. You can also initialize a Map with an array of key-value pairs.
1const myMap = new Map();2console.log(myMap);
Map(0) {}1const initialData = [2['name', 'Alice'],3[1, 'One'],4[{}, 'Object']5];67const myMap = new Map(initialData);8console.log(myMap);
Map(3) {
'name' => 'Alice',
1 => 'One',
{} => 'Object'
}You can add entries to a Map using the set() method and retrieve them using the get() method.
1const myMap = new Map();2myMap.set('name', 'Alice');3myMap.set(1, 'One');4console.log(myMap);
Map(2) { 'name' => 'Alice', 1 => 'One' }1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2console.log(myMap.get('name')); // Output: Alice3console.log(myMap.get(1)); // Output: One
Alice One
You can check if a key exists in a Map using the has() method.
1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2console.log(myMap.has('name')); // Output: true3console.log(myMap.has(2)); // Output: false
true false
You can remove entries from a Map using the delete() method and clear all entries using the clear() method.
1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2myMap.delete('name');3console.log(myMap);
Map(1) { 1 => 'One' }1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2myMap.clear();3console.log(myMap);
Map(0) {}You can iterate over a Map using various methods like forEach(), for...of, and more.
1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2myMap.forEach((value, key) => {3console.log(`${key} => ${value}`);4});
name => Alice 1 => One
1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2for (let [key, value] of myMap) {3console.log(`${key} => ${value}`);4}
name => Alice 1 => One
You can get the number of entries in a Map using the size property.
1const myMap = new Map([['name', 'Alice'], [1, 'One']]);2console.log(myMap.size); // Output: 2
2
Let's create a simple program that uses a Map to store and manage user data.
1const users = new Map();23function addUser(id, name) {4if (users.has(id)) {5console.log(`User with ID ${id} already exists.`);6} else {7users.set(id, name);8console.log(`User ${name} added with ID ${id}.`);9}10}1112function getUser(id) {13const user = users.get(id);14if (user) {15console.log(`User found: ${user}`);16} else {17console.log(`No user found with ID ${id}.`);18}19}2021addUser(1, 'Alice');22addUser(2, 'Bob');23getUser(1);24getUser(3);
User Alice added with ID 1. User Bob added with ID 2. User found: Alice No user found with ID 3.
| Concept | Description |
|---|---|
Map | A collection of key-value pairs where keys can be of any type. |
set() | Adds a new entry to the map. |
get() | Retrieves the value associated with a given key. |
has() | Checks if a key exists in the map. |
delete() | Removes an entry from the map. |
clear() | Removes all entries from the map. |
forEach() | Iterates over each entry in the map using a callback function. |
for...of | Iterates over each entry in the map using a for loop. |
size | Returns the number of entries in the map. |
Now that you have a solid understanding of JavaScript Maps, it's time to explore how to work with dates and times in JavaScript. In the next tutorial, we will cover the Date object and various methods for manipulating and formatting dates. Stay tuned!