Lesson 4
Applying Maps for Real-World Challenges in TypeScript
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 TypeScript, with enhanced readability and error-checking through type annotations.

TypeScript
1// Type definitions for book details 2type BookDetails = Map<string, string>; 3 4// Initializing a Map with type annotations 5let libraryCatalog: Map<string, BookDetails> = new Map(); 6 7// Details of a book 8let bookId: string = "123"; 9let bookDetails: BookDetails = new Map([ 10 ["title", "To Kill a Mockingbird"], 11 ["author", "Harper Lee"], 12 ["year_published", "1960"] 13]); 14 15libraryCatalog.set(bookId, bookDetails); // Adding a book to library catalog, where the value itself is a Map 16 17// Searching for a book 18if (libraryCatalog.has(bookId)) { 19 let details: BookDetails | undefined = libraryCatalog.get(bookId); 20 if (details) { 21 console.log(`Title: ${details.get("title")}, Author: ${details.get("author")}, Year Published: ${details.get("year_published")}`); 22 } 23} 24 25libraryCatalog.delete(bookId); // Removing a book from the library

With the use of TypeScript, our Maps make the task of cataloging books in the library more efficient, readable, and type-safe.

Real-World Application: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. By employing a Map, where each name is a unique key, and the frequency of that name serves as the associated value, we can efficiently count votes. Below is the TypeScript code to achieve this:

TypeScript
1let votesList: string[] = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"]; // Cast votes 2let voteCounts: Map<string, number> = 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

This TypeScript implementation of Maps offers a succinct and robust method for counting votes, providing both clarity and safety through type annotations.

Real-World Application: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a Map where product names are keys, and quantities are values. This approach facilitates easy management of store inventories with TypeScript for added type safety.

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

Using TypeScript's Maps, we achieve more robust and maintainable solutions for managing inventory data.

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 in TypeScript, we not only optimize data management but also enhance code clarity and reliability with type-checking features. 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.