Angular - Array calling only first item

Asked

Viewed 62 times

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?

  • 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

  • is returning you the first why there is a return in the for. With this the first interaction is finalized. understand about Ties and iterations

2 answers

0


About such a problem in for, there is return so in the first interaction when arriving at it is finalized the execution of this method.

about Ties and iterations

From what I understand, I imagine you’ll have a list of pastries pastelDataList in which the user selects and thus the value of each will be added total. Well, the code is confused in understanding to deepen better, but we will exactly in doubt about the variable total.

simply you can use the forEach, example:

interface pastelData {
  name: string;
  price: number;
  status: string;
}

//nomenclatura mudaria para setPricePastel
getPricePastel(pastelDataList: pastelData[]) {
  this.total = 0; //zerar
  this.pastelData.forEach((data)=>{
     this.total += data.price;
  })
  this.nextPastel.next(this.pastelData);
}

0

You are stopping the repeat loop on the first index since you have a Return that for the execution of the for.

After removing Return will calculate the total value, you could do so:

let valorTotal: number = 0;
for (let i = 0; i < pastelData.length; i++) {
    console.log(pastelData[i].price);    
    valorTotal += (pastelData[i].price);
    this.nextPastel.next(this.pastelData);        
}
console.log(valorTotal)//Apenas para mostrar o valor a ser retornado
return valorTotal;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.