Home > OS >  how to sum values ​of arrays in the same position
how to sum values ​of arrays in the same position

Time:04-19

I have this line of code, how could I add the // "0[37][16]" to total 53 for example?

updateTotalMedidas() {
    this.ɵcart.medidaTotal = 0;
    for (const item of this.ɵcart.item) {
      let row_cm = (item.medidas[0] * item.medidas[1] * item.medidas[2]) * item.quantidade; // (3) [84, 40, 15] and (3) [30, 18, 8]
      let totalCmCubico = row_cm  // 50400 and 4320
      let raiz_cubica =  Math.round(Math.cbrt(totalCmCubico))  // 37 and  16
      this.ɵcart.medidaTotal  = JSON.stringify([raiz_cubica]); // "0[37][16]"

    }
  }

if I remove the "[]" from this.ɵcart.medidaTotal it becomes 3716, so I put the [], but I don't understand why it stays in that format

CodePudding user response:

This happens because you put everything inside JSON.stringify this converts objects into JSON strings. So in your current code mediaTotal will be first 0 then "37" and at last "3716". No need for that in your code.

You can just do this, because raiz_cubica is a number.

this.ɵcart.medidaTotal  = raiz_cubica;
  • Related