javascriptfor-loopdata-layer

How to add multiple datalayer parameters with for loop?


I have a datalayer with parameter "Price" which I want to add, e.g. 799 + 95 + 95.

enter image description here

DigitalData[0].Cart.Items[0].Price 

Returns "799.00"

Believe one could JavaScript For Loop to achieve the subtraction of price that I'm looking for. But I'm not familiar how to type this scenario. Am I going towards the right direction? See below code?

for (i = 0; i < items.length; i++) { 
    DigitalData[0].Cart.Items[i].Price;
}

Solution

  • You need a variable to store the sum:

    var sum = 0;
    var items = DigitalData[0].Cart.Items;
    for (i = 0; i < items.length; i++) { 
        sum += parseInt(items[i].Price);
    }