Forum Discussion

vgautam64's avatar
vgautam64
New Contributor III
2 years ago
Solved

Reduce an array of objects into a single object with the sum of fields as the result

I have an array of the following form:

I want to convert this array of objects into a single object that has the same fields along with Debit_Amount and Credit_Amount fields summed up.

Any help is appreciated!

  • vgautam64 - I believe this solves your question in the simplest way I can think of.  Use a Mapper snap with the following configuration:

    It appears to me that you only need the field elements and values from the first group element: $group[0]

    The two jsonPath statements are simply pulling all values for the Debit and Credit Amounts into an array of numbers

    Finally, the Array.reduce() method is used to aggregate that list of numbers into a final total.

    Hope this helps!

     

7 Replies

  • christwr's avatar
    christwr
    Contributor III

    Not sure if it’s “legit” from SnapLogic’s perspective, but you can implement looping between parent/child pipelines. Have the child pipeline exposed via triggered task, then have parent pipeline use REST GET snap to call the child pipeline triggered task, and use the REST GET’s pagination features (Has Next & Next URL) to evaluate some criteria and re-GET the child pipeline until criteria is false.