const inventory = new Map([
["sword", { quantity: 3, weight: 15 }],
["shield", { quantity: 1, weight: 25 }],
["potion", { quantity: 10, weight: 2 }],
]);
inventory.set("bow", { quantity: 2, weight: 8 });
inventory.delete("shield");
const totalWeight = [...inventory.entries()].reduce((acc, [item, { quantity, weight }]) => {
return acc + quantity * weight;
}, 0);
const heaviestItem = [...inventory.entries()].reduce((max, [item, data]) =>
data.weight > max.weight ? { name: item, ...data } : max,
{ name: "", weight: 0 }
);
console.log(inventory.size, totalWeight, heaviestItem.name);
Post #3190
2.46K
CHALLENGE
- ❤ 4
- 👍 2