0% found this document useful (0 votes)
15 views

JavaScript_Maps_Presentation

Uploaded by

Abishek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

JavaScript_Maps_Presentation

Uploaded by

Abishek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Manipulating and Accessing Map

Data Types in JavaScript


Team Members: [Your Names]
Date: [Presentation Date]
Introduction
• • What are Maps in JavaScript?
• - A collection of key-value pairs.
• - Keys can be of any data type.
• - Maintains insertion order.
• • Why Use Maps?
• - Efficient data retrieval.
• - More versatile than objects for key-value
mapping.
Creating a Map
• Code Example:
• const myMap = new Map();

• Adding Entries:
• myMap.set('key1', 'value1');
• myMap.set('key2', 'value2');
Accessing Map Data
• • Getting Values:
• const value = myMap.get('key1');

• • Checking Keys:
• myMap.has('key1'); // true

• • Size of the Map:


• myMap.size; // 2
Iterating Over Maps
• • Using forEach:
• myMap.forEach((value, key) => {
• console.log(key, value);
• });

• • Using for...of:
• for (const [key, value] of myMap) {
• console.log(key, value);
• }
Modifying Maps
• • Deleting Entries:
• myMap.delete('key1');

• • Clearing the Map:


• myMap.clear();
Use Cases of Maps
• • Storing user sessions.
• • Tracking inventory in e-commerce.
• • Caching results of computations.
Advantages of Maps
• • Compared to Objects:
• - Keys can be any type, including objects and
functions.
• - Preserves the order of insertion.
• - Easier size determination using `.size`.
Practical Example
• Task: Counting word frequency in a string.

• const wordCount = new Map();


• const words = 'hello world hello'.split(' ');
• words.forEach(word => {
• wordCount.set(word, (wordCount.get(word)
|| 0) + 1);
• });
• console.log(wordCount); // Map(2) { 'hello' =>
Conclusion
• • Key Points:
• - Maps are flexible and powerful for
managing key-value pairs.
• - Provide better performance and more
features compared to objects.

• • Questions?

You might also like