Food Ordering App: Cart Implementation in JavaScript
The cart is the hardest part. Here is how to implement add, remove, quantity, and total.
Food Ordering App: Cart Implementation in JavaScript
The cart is the most important part of the food ordering app. Here is how to implement it cleanly.
Cart Data Structure
let cart = {}; // { itemId: quantity } `` Using an object with item ID as key and quantity as value is simple and efficient. ### Add to Cart ```js function addToCart(itemId) { cart[itemId] = (cart[itemId] || 0) + 1; renderCart(); } `` If the item is not in the cart, start at 0 and add 1. If it is, increment. ### Remove from Cart ```js function removeFromCart(itemId) { delete cart[itemId]; renderCart(); } `` ### Change Quantity ```js function changeQuantity(itemId, delta) { cart[itemId] = (cart[itemId] || 0) + delta; if (cart[itemId] <= 0) delete cart[itemId]; renderCart(); } `` If quantity drops to 0 or below, remove the item from the cart. ### Calculate Total ```js function calculateTotal() { return Object.entries(cart).reduce((sum, [id, qty]) => { const item = menu.find((m) => m.id === parseInt(id)); return sum + item.price * qty; }, 0); } `` ### Render Cart ```js function renderCart() { const cartItems = Object.entries(cart); if (cartItems.length === 0) { cartDiv.innerHTML = "<p>Your cart is empty</p>"; return; } cartItems.forEach(([id, qty]) => { const item = menu.find((m) => m.id === parseInt(id)); // render item with +/- buttons and subtotal }); // render total and checkout button } `` ### The Takeaway Cart implementation: use an object { itemId: quantity }. Add increments, remove deletes, changeQuantity adjusts and removes at 0. Calculate total by summing price * qty. Render the cart with +/- buttons, subtotals, and a total. Handle empty cart.
Use an object mapping item ID to quantity: cart = { 1: 2, 3: 1 }. addToCart increments, removeFromCart deletes, changeQuantity adjusts and removes at 0. Calculate total by summing price * qty for each item.
An object with item ID as key and quantity as value: { itemId: quantity }. This gives O(1) lookup, add, and remove. Alternatively, an array of { id, quantity } objects, but the object is simpler for this use case.
When decrementing, check if the quantity drops to 0 or below. If so, delete the item from the cart: if (cart[id] <= 0) delete cart[id]. This keeps the cart clean and the total accurate.
Sum price * quantity for each item: Object.entries(cart).reduce((sum, [id, qty]) => { const item = menu.find(m => m.id === parseInt(id)); return sum + item.price * qty; }, 0).
Check if Object.keys(cart).length === 0. If so, show a message like 'Your cart is empty' instead of the cart items and total. This shows attention to UX.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

