When building a shopping cart, you need to store items and update them (change quantity, remove item, etc.). You can do that with an array, an object, or a Map — but they don’t behave the same.
1) Using an Array
const cart = [
{ id: 1, name: "first", price: 10 },
{ id: 2, name: "second", price: 20 },
];
Why it can be annoying
If you want to find or update an item by id, you usually need to loop:
const item = cart.find((p) => p.id === 2);
That’s fine for small carts, but as the cart grows, repeated .find() / .filter() calls add extra work.
2) Using an Object
const cart = {
"id-1": { id: 1, name: "first", price: 10 },
"id-2": { id: 2, name: "second", price: 20 },
};
Pros
- Fast lookups by key:
cart["id-2"]; // { id: 2, name: "second", price: 20 }
Downsides
- Keys are basically strings only
- You need to be careful with object edge-cases (prototype stuff) and iteration patterns
3) Using new Map() (Recommended for cart-like data)
A Map stores key → value pairs, like an object — but it’s designed for this use case.
Why Map is nice
- Easy and clean lookups:
map.get(id) - Safe iteration
- Keys can be any type (number, object, etc.)
- Built-in helpers:
set,get,has,delete,size
Map Example: Shopping Cart
// create a new Map
const cart = new Map();
// add items
cart.set(1, { id: 1, name: "Apple", price: 0.5, quantity: 3 });
cart.set(2, { id: 2, name: "Banana", price: 0.3, quantity: 6 });
cart.set(3, { id: 3, name: "Orange", price: 0.4, quantity: 4 });
// read item
console.log(cart.get(1));
// { id: 1, name: 'Apple', price: 0.5, quantity: 3 }
// check existence
console.log(cart.has(99)); // false
// size
console.log(cart.size); // 3
// remove item
cart.delete(2);
// loop
for (const [id, item] of cart) {
console.log(id, item.name, item.price, item.quantity);
}
Converting Array → Map (and back)
Array of items
const items = [
{ id: 1, name: "first", price: 10 },
{ id: 2, name: "second", price: 20 },
];
Convert to [key, value] pairs
const cartItems = items.map((item) => [item.id, item]);
console.log(cartItems);
// [
// [1, { id: 1, name: 'first', price: 10 }],
// [2, { id: 2, name: 'second', price: 20 }],
// ]
Create a Map from pairs
const cart = new Map(cartItems);
Convert Map back to array
const cartArray = Array.from(cart.entries());
console.log(cartArray);
// [
// [1, { id: 1, name: 'first', price: 10 }],
// [2, { id: 2, name: 'second', price: 20 }],
// ]
Credits: John's Smilga's course
Top comments (4)
Here’s a tiny “update quantity” example that shows why
Mapfeels cleaner for a cart 👇Update quantity with an Array (usually needs a lookup)
Update quantity with a Map (direct by key)
With
Map, theidis the key, so updating is basically: get → set (no searching).One extra note about using plain objects for a cart 👇
Object edge-cases (prototype stuff)
Objects inherit from
Object.prototype, so some keys can behave weirdly or collide with built-ins:If you really want an object as a dictionary, a safer option is:
Iteration patterns
With objects you typically use
Object.keys()/Object.entries()(notfor...ofdirectly):So objects can work, but you need to be mindful of prototype quirks + how you iterate. Maps avoid both problems and give you a cleaner API for “data keyed by id”.
Quick summary for anyone skimming:
idusually means doing.find()/.filter()every time (fine for small carts, gets annoying as it grows).Map is a really nice fit for a cart because it’s built for key/value storage:
set / get / has / deletesizeis built-inSmall example (using numeric product IDs):
And if you already have an array, converting is super clean:
Map is underrated for this kind of “lookup/update by id” data.
Extra (Video)