LearnFrenzy
Sign in Take a free test

Spread and Rest Operators · Spread and Rest Coding Problems · Question 5

Clone and enrich records with spread in JavaScript Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

Add a computed total and a status to each order without changing the original order objects.

Solution
main.js
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]);

Output Verified by running it on Node.js

{ 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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links