Caclulations based on JSON or Repeatable List

Im wondering what the best way is to perform some calculations based on JSON Data.
Im building an app for creating quotes. The dataset for the quote is stored in a JSON and is displayed in a repeatable List. I have chunked together a calculation that runs in a JS query but I'm sure there's a better way as I don't want to have to execute JS each time to recalculate, Unless this can all be done in 1 query. I'm most hopeful this can be done in a transformer.

1 Field i would like to sum within the repeatable is {{itemLineTotal.value}}

If there's a way to calc based off of the item JSON, I feel this may be more Ideal as I would want it to run calculations for Items, Categories, and a quoteTotal. it follows this schema.

"id": 1132,
"name": "Test Quote",
"quoteTotal": null,
"categories": [
"categoryName": "Hardware and Installation",
"categoryId": 1,
"categorySubtotal": null,
"categoryFitHrs": null,
"categoryLaborPrice": null,
"items": [
"itemId": 1,
"itemProductId": 123,
"itemQty": 1,
"itemMarkup": 66.7,
"itemPrice": 150,
"itemFitTime": 20,
"itemFitMultiplier": 1,
"itemLineTotal": 250,
"itemSubRequired": false,
"itemSubPrepay": false

I think you'll want to use reduce here. Something similar to this example. Array.prototype.reduce() - JavaScript | MDN

Your code will look something like

object['items'].reduce((sum, item) => sum + item['itemLineTotal'], 0)

If you want to sum multiple fields from the item then return an object in each reduce loop instead, with a key for each summed field.

Note that a transformer is still running JS code. In fact it is constantly running any time the state of the inputs change, so it could be even more cumbersome than a JS script that runs when you tell it to.

That did the trick perfectly. Thanks so much!

1 Like