Forum Discussion

spinaka's avatar
spinaka
New Contributor III
4 years ago
Solved

Cumulative sum of a value in an array

Hi Everyone, I need help on an expression solution. The “sum” should be the sum of the value(s) of “key2” from previous records till the current record within the array. I’m trying to make it usi...
  • bojanvelevski's avatar
    bojanvelevski
    4 years ago

    Here’s a corrected version of the expression:

    $array.map((x,index)=> index==0 ? x.extend({sum:x.key2}) : x.extend({sum:$array.slice(0,index+1).reduce((acc,curr)=> acc+curr.key2,0)}))

    Let me know if this works for you, and I will explain in details on how the expression works.