Skip to content

Latest commit

 

History

History

README.md

Maps

The Map is a built-in data structure that allows you to store key-value pairs, where keys and values can be of any data type. Unlike objects, Map provides a clear separation between keys and values, and it allows you to use any value (including objects) as keys, while objects use only strings or Symbols as keys.

Here's how you can work with Map in JavaScript:

Creating a Map:

// Creating a new Map
var myMap = new Map();

// Adding key-value pairs to the Map
myMap.set("name", "John");
myMap.set("age", 30);
myMap.set("occupation", "Engineer");

Getting Values from a Map:

var name = myMap.get("name"); // "John"
var age = myMap.get("age");   // 30

Checking for Key Existence:

console.log(myMap.has("name")); // true
console.log(myMap.has("gender")); // false

Deleting Entries:

myMap.delete("age");

Iterating through a Map:

for (var [key, value] of myMap) {
  console.log(key, value);
}

Getting the Size:

console.log("Size of the map:", myMap.size);

Clearing the Map:

myMap.clear();

Map has a few notable advantages over plain objects:

  1. It allows any type of value as keys.
  2. It maintains the order of insertion, making it suitable for cases where the order of keys is important.
  3. It provides built-in methods for various operations like getting the size, checking for key existence, and more.

Example illustrating these concepts:

var person = new Map();
person.set("name", "Alice");
person.set("age", 25);
person.set("email", "alice@example.com");

console.log("Name:", person.get("name"));

console.log("Keys in the Map:");
for (var key of person.keys()) {
  console.log(key);
}

console.log("Values in the Map:");
for (var value of person.values()) {
  console.log(value);
}

console.log("Entries in the Map:");
for (var [key, value] of person.entries()) {
  console.log(key, value);
}

Certainly! Here's an example of how you can use the Map data structure in JavaScript:

// Creating a new Map
var employeeInfo = new Map();

// Adding key-value pairs to the Map
employeeInfo.set("name", "John Doe");
employeeInfo.set("age", 30);
employeeInfo.set("position", "Software Developer");

// Getting values from the Map
var name = employeeInfo.get("name");
var age = employeeInfo.get("age");
var position = employeeInfo.get("position");

// Checking for key existence
var hasSalary = employeeInfo.has("salary"); // false

// Iterating through the Map
for (var [key, value] of employeeInfo) {
  console.log(key + ": " + value);
}

// Getting the size of the Map
var mapSize = employeeInfo.size;

// Deleting a key-value pair
employeeInfo.delete("position");

// Clearing the Map
employeeInfo.clear();

console.log("Employee Info Map after clearing:", employeeInfo);

Output:

name: John Doe
age: 30
position: Software Developer
Employee Info Map after clearing: Map {}

In this example, a Map named employeeInfo is created to store information about an employee. Key-value pairs are added to the map using the set() method. The values are retrieved using the get() method, and the existence of a key is checked using the has() method. The for...of loop is used to iterate through the map's entries.

The map's size is obtained using the size property, and a key-value pair is deleted using the delete() method. Finally, the map is cleared using the clear() method.

JavaScript's Map provides a clean and efficient way to manage key-value pairs and is particularly useful when you need to preserve insertion order, store unique keys, and have more flexibility with key types compared to regular objects.