Sum of dynamic objects in array

Hi, I have the below array whose object will increase and decrease dynamically. and each time I need to
get the total by doing “price” * “quant”.

[
    {
        "id": "95",
        "price": 100,
        "quant": 1
    },
    {
        "tovar_id": "73",
        "price": 4,
        "quant": 2
    },
    {
        "tovar_id": "75",
        "price": 10,
        "quant": 2
    }
]

I need something like
total = 28

I have tried this way and it reset the total value each time a new object added to it.

total(){

    if (this.sell.length > 0) {

      for(let i = 0; i < this.sell.length; i++){

        this.itemstotal = this.sell[i]['quant']*this.sell[i]['price'];

        break;

      }

      console.log("TOTAL ",this.itemstotal);

    }

  }

Thanks in advance.

total(){

    this.itemstotal=0;

    if (this.sell.length > 0) {

      for(let i = 0; i < this.sell.length; i++){

        this.itemstotal += this.sell[i]['quant']*this.sell[i]['price'];

      }

 

    }

     console.log("TOTAL ",this.itemstotal);

  }

This works better?

1 Like

Thanks, it worked for me also:)