JSView / JSRender calculate the total quantity from for loop attributes

120 Views Asked by At

I have for loop items as follows:

 "itemDtls": [
        {
            "ID": A1299,
            "Name": "Fresh Tomato",
            "Quantity": 2,
        },
        {
            "ID": A1230,
            "Name": "Spaghetti",
            "Quantity": 1,
        },
        {
            "ID": A1335,
            "Name": "Granola",
            "Quantity": 5,
        },
    ],

I did a for-loop to iterate these items:

    {{for itemDtls}}
    <col>{{>Name}}</col>
    <col>{{>Quantity}}</col>
    {{/for}}

This ends up fine, but I would like to also get the total of the 'Quantity' (that would be 8)

How to achieve that?

1

There are 1 best solutions below

0
On

Never worked with jsviews but if you want it in the view maybe try something like this:

{{itemDtls.reduce((accum, reducer) => accum + reducer.Quantity, 0)}}

If you don't want it in the view, I think you can compute it before hand, store it in a variable and show that variable.