我有一个函数,其任务是计算折扣后产品的价格。然而我得到的结果是没有这个值(totalPrice)的对象。我在哪里犯了错误?
const arrayObj = [
{
basePrice: 12,
discount: 3,
},
{
basePrice: 12,
discount: 2,
},
{
basePrice: 8,
discount: 2,
},
];
interface Product {
basePrice: number;
discount: number;
totalPrice?: number;
}
const countTotalPrice = (products: Product[]): Product[] => {
const calculateTotalPrice = (
basePrice: number,
discount: number,
totalPrice?: number
): void => {
totalPrice = basePrice - discount;
};
products.forEach((product) =>
calculateTotalPrice(
product.basePrice,
product.discount,
product.totalPrice
)
);
return products;
};
console.log(countTotalPrice(arrayObj));