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.
let storeInventory = new Map();
storeInventory.set("Apples", 100);
storeInventory.set("Bananas", 80);
storeInventory.set("Oranges", 90); // Initializing an inventory
storeInventory.set("Apples", storeInventory.get("Apples") + 20); // Updating the number of apples in inventory
let prod = "Apples"; // A product to be checked
console.log(`Total ${prod} in stock: ${storeInventory.get(prod)}`);
// Check if a product is in stock
prod = "Mangoes";
if (storeInventory.has(prod)) { // If mangoes exist in inventory
console.log(`${prod} are in stock.`);
} else { // If mangoes don't exist in inventory
console.log(`${prod} are out of stock.`);
}
When managing inventory data, Maps offer an efficient solution by allowing you to easily manipulate and query your store's stock.