cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Sum in a loop

nirupama
New Contributor II

Hi,
I have an default value for field $sum=390 and 10 input records(count may increase/decrease).
Requirement :Each row should get $sum=$sum+10. if 1st row having sum=400, 2nd sum=410โ€ฆ
Please suggest.

1 ACCEPTED SOLUTION

pmancevski
New Contributor III

Hi @nirupama,

You can use โ€œMapperโ€ snap, with following settings:

image

Thanks,
Pero M.

View solution in original post

5 REPLIES 5

pmancevski
New Contributor III

Hi @nirupama,

You can use โ€œMapperโ€ snap, with following settings:

image

Thanks,
Pero M.

nirupama
New Contributor II

@pmancevski Thank you for the quick response. It helped.

nirupama
New Contributor II

@pmancevski How to apply the same logic on group by values. Sample data is attached below. Sum should be incremented by 10 for each row.
[
{
โ€œgroupByโ€: {
โ€œnumberโ€: โ€œ1115122โ€
},
โ€œgroupโ€: [
{
โ€œnameโ€: โ€œTDโ€,
โ€œnumberโ€: โ€œ1115122โ€,
โ€œsumโ€: โ€œ390โ€
},
{
โ€œnameโ€: โ€œTDโ€,
โ€œnumberโ€: โ€œ1115122โ€,
โ€œsumโ€: โ€œ390โ€
}
]
},
{
โ€œgroupByโ€: {
โ€œnumberโ€: โ€œ1115982โ€
},
โ€œgroupโ€: [
{
โ€œnameโ€: โ€œTDโ€,
โ€œnumberโ€: โ€œ1115982โ€,
โ€œsumโ€: โ€œ390โ€
}
]
},
{
โ€œgroupByโ€: {
โ€œnumberโ€: โ€œ1115962โ€
},
โ€œgroupโ€: [
{
โ€œnameโ€: โ€œTDโ€,
โ€œnumberโ€: โ€œ1115962โ€,
โ€œsumโ€: โ€œ390โ€
}
]
}
]

required output:
1st group : sum=400, 410
2nd group : sum=400
3rd group : sum=400

pmancevski
New Contributor III

Hi,

Probably there is simpler solution, but at the moment i can think only this:

image

Replace previous expression with this:
$group.map((value, index) => value.extend({โ€œsumโ€ : parseInt(value.sum) + ((index + 1) * 10)}))

We are extending the object with same field, because previously we had that field name, it will be only overwritten.

Thanks,
Pero M.