Add up from response

Hello everyone.

From a RESTapi I get the following response:

{
"response": "success",
{
"order_id": 8133122,
"order_products": [
{
"price": 14.99,
"profit": 5.92347,
}
]
},
{
"order_id": 8133123,
"order_products": [
{
"price": 19.99,
"profit": 8.8217,
}
]
},

Of course there is more to come.
The question is how can I add up the Profit of all orders in a statistic component?
Love to hear for you guys!

Hey @Matthijspo!

It looks like you can use some JavaScript here to unpack and sum your data! Not sure what the full structure of your data is so this won't be the precise syntax, there are also a number of variations on how you might do this so I'm curious to see if anyone else has suggestions. At any rate, one option is to do something like this:

_.sumBy(query.data.orders.flatMap(order => order.order_products), order => order.profit);

First, create a master list of products (assuming there can be multiple products per order) by extracting order_products from each order and then flattening the array with query.data.orders.flatMap(order => order.order_products). Then, use the lodash _.sumBy function to sum over the profit property on each of those objects by specifying it with order => order.profit.

Let me know if that works!