items: [
{ name: 'apple', quantity: 1 },
{ name: 'banana', quantity: 2 },
{ name: 'apple', quantity: 3 },
{ name: 'orange', quantity: 4 }
]
mergedItems() {
const map = {};
this.items.forEach(item => {
if (map.hasOwnProperty(item.name)) {
map[item.name].quantity += item.quantity;
} else {
map[item.name] = { ...item };
}
});
return Object.values(map);
}