Lesson 4
Applying Maps to Real-World Challenges in JavaScript
Introduction and Goal Setting

Hello there! In this lesson, we will apply Maps to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Application: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values. This approach allows us to add, search for, and remove books from our library catalog using just a few lines of JavaScript code.

JavaScript
1// Initializing a Map 2let libraryCatalog = new Map(); 3 4// Details of a book 5let bookId = "123"; 6let bookDetails = new Map(); 7bookDetails.set("title", "To Kill a Mockingbird"); 8bookDetails.set("author", "Harper Lee"); 9bookDetails.set("year_published", "1960"); 10 11libraryCatalog.set(bookId, bookDetails); // Adding a book to library catalog, where value itself is a Map 12 13// Searching for a book 14if (libraryCatalog.has(bookId)) { 15 let details = libraryCatalog.get(bookId); 16 console.log(`Title: ${details.get("title")}, Author: ${details.get("author")}, Year Published: ${details.get("year_published")}`); 17} 18 19libraryCatalog.delete(bookId); // Removing a book from the library

As you can see, Maps make the task of cataloging books in the library simpler and more efficient. You can dynamically add a new book with its unique ID, retrieve its information, and even remove it if necessary.

Real-World Application: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a Map, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some JavaScript code to better understand this.

JavaScript
1let votesList = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"]; // Cast votes 2let voteCounts = new Map(); // Initializing a Map 3 4// Counting the votes 5for (let name of votesList) { 6 voteCounts.set(name, (voteCounts.get(name) || 0) + 1); 7} 8 9for (let [candidate, count] of voteCounts) { 10 console.log(`${candidate}: ${count}`); 11} 12// Prints: Alice: 3, Bob: 2, Charlie: 1

Maps facilitate the efficient counting of votes. Using the Map, we can iterate over the votes, increment counts, and ultimately print the results in a straightforward and efficient manner.

Real-World Application: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a Map in which product names are keys, and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

JavaScript
1let storeInventory = new Map(); 2storeInventory.set("Apples", 100); 3storeInventory.set("Bananas", 80); 4storeInventory.set("Oranges", 90); // Initializing an inventory 5 6storeInventory.set("Apples", storeInventory.get("Apples") + 20); // Updating the number of apples in inventory 7 8let prod = "Apples"; // A product to be checked 9console.log(`Total ${prod} in stock: ${storeInventory.get(prod)}`); 10 11// Check if a product is in stock 12prod = "Mangoes"; 13if (storeInventory.has(prod)) { // If mangoes exist in inventory 14 console.log(`${prod} are in stock.`); 15} else { // If mangoes don't exist in inventory 16 console.log(`${prod} are out of stock.`); 17}

When managing inventory data, Maps offer an efficient solution by allowing you to easily manipulate and query your store's stock.

Conclusion

Maps are incredibly versatile and provide efficient ways to handle real-world tasks such as cataloging books, counting votes, and managing inventories. By employing Maps, you can simplify and optimize data management, making your JavaScript code more efficient and easier to maintain.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your Map problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.