0
I am trying to calculate an Array to display the total value by adding an item to my cart.
My array looks like this: I use the Status to inform in the view that the item has been added to the cart.
public pastelData = [{
name: 'Pastel de Calabresa',
price: 7.50,
status: 'false', }]
That way when clicking I run the service that changes the status to added to the cart and also captures the price.
addItems(pastelData) {
this.sharedSerivce.setItemData(pastelData);
pastelData.status = 'Adicionado';
this.getPricePastel(this.pastelData);
}
I created the following function to pick up the prices of the pastries:
getPricePastel(pastelData) {
for (const index in pastelData) {
}
for (let i = 0; i < pastelData.length; i++) {
console.log(pastelData[i].price);
this.total += (pastelData[i].price);
console.log(this.total);
this.nextPastel.next(this.pastelData);
return;
}
}
It behaves like this: If I leave Return at the end, it takes all addItems calls only at the first price. If I remove Return, all calls take the total price[]. And so, I can’t call the price equivalent to the item the user adds to the cart
It’s a study project;
Got confused your question, vc has an array of objects that owns a property with prices and wants to know the total prices is this?
– LeAndrade
sorry I haven’t been very clear. That’s right, though, I want to know the total based on the items I called, not the total of all. For example: My array has the following composition: [ A, price:1 B, price:2 C, price:3] I don’t want to call them all at once. However, the way I did it calls only the first. I’m not getting access to his other positions. Because I created a cart, inside it I have the "add items" function that calls my product, value etc.. but when adding up the total I’m not getting straight flame
– Deivide Paulino
is returning you the first why there is a
return
in thefor
. With this the first interaction is finalized. understand about Ties and iterations– darkziul