GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Front-end-Development-Tutorial/JavaScript at main · Pankaj-Str/Front-end-Development-Tutorial

Expand file tree

/

JavaScript

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

JavaScript

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
01 Day Basic
01 Day Basic
 
 
02 Day Operator
02 Day Operator
 
 
04 Day Function
04 Day Function
 
 
05 Day Objects
05 Day Objects
 
 
06 Day Events
06 Day Events
 
 
08 Day String Methods
08 Day String Methods
 
 
09 Day Numbers
09 Day Numbers
 
 
10 Day Array
10 Day Array
 
 
11 Day Date
11 Day Date
 
 
12 Day Math
12 Day Math
 
 
13 Day If Else
13 Day If Else
 
 
14 Day Switch
14 Day Switch
 
 
15 Day Loop
15 Day Loop
 
 
16 Day Sets
16 Day Sets
 
 
Example
Example
 
 
Lecture 01
Lecture 01
 
 
Questions
Questions
 
 
README.md
README.md
 
 

README.md

Outline

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", "[email protected]");

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.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.