Question 5
Back to Exercise
Add a computed total and a status to each order without changing the original order objects.
Solution
const orders = [
{ id: 1, qty: 3, price: 120 },
{ id: 2, qty: 1, price: 999 },
];
const enriched = orders.map((o) => ({
...o,
total: o.qty * o.price,
status: o.qty * o.price > 500 ? "free shipping" : "₹40 shipping",
}));
console.log(enriched[0]);
console.log(enriched[1].status, "total" in orders[0]);
{ id: 1, qty: 3, price: 120, total: 360, status: '₹40 shipping' }
free shipping false
{ ...o, extra } builds a new object per order, so the originals stay untouched ("total" in orders[0] is false).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!