기존 코드
const getTotal = () => {
let total = {
price: 0,
quantity: 0,
};
for (let i = 0; i < cartItems.length; i++) {
total.quantity = total.quantity + cartItems[i].quantity;
}
for (let j = 0; j < renderItems.length; j++) {
let price = renderItems.filter((el) => el.id === cartItems[j].itemId)[0]
.price;
total.price = total.price + price * cartItems[j].quantity;
}
return total;
};
코드 수정
for (let i = 0; i < cartItems.length; i++) {
total.quantity = total.quantity + cartItems[i].quantity;
}
for (let j = 0; j < cartItems.length; j++) {
let price = renderItems.filter((el) => el.id === cartItems[j].itemId)[0]
.price;
total.price = total.price + price * cartItems[j].quantity;
}
for문에서 같은 cartItems 배열로 변경 가능
const getTotal = () => {
let total = {
price: 0,
quantity: 0,
};
for (let i = 0; i < cartItems.length; i++) {
let price = renderItems.filter((el) => el.id === cartItems[i].itemId)[0]
.price;
total.quantity = total.quantity + cartItems[i].quantity;
total.price = total.price + price * cartItems[i].quantity;
}
return total;
};