Reputation: 788
The below code isn't displaying all my items quantity, it's only displaying the first item
import {ShoppingCartItem} from './shopping-cart-item';
export class ShoppingCart {
constructor(public items: ShoppingCartItem[]) {}
get totalItemsCount() {
let count = 0;
for (const productId in this.items) {
if (this.items.hasOwnProperty(productId)) {
count += this.items[productId].quantity;
return count;
}
}
}
}
Upvotes: 1
Views: 35
Reputation: 222700
Because you are returning inside the loop, Change it as follows,
get totalItemsCount() {
let count = 0;
for (const productId in this.items) {
if (this.items.hasOwnProperty(productId)) {
count += this.items[productId].quantity;
}
}
return count;
}
Upvotes: 1