In this array of a list of devices, you would need to delete from the array when any item was zeroed out
[
{
"valueTotal": "6.50"
},
{
"bread": "001",
"value": "3.00"
},
{
"milk": "002",
"value": "3.50"
},
{
"coffe": "003",
"value": "0.00"
}
]
CodePudding user response:
Assuming you intend to remove the items in the input, whose "value" field is 0 and then get the totalValue. Here is a quick one I have come up with(could be improved).
%dw 2.0
output application/json
//filter the items whose value is zero
var filteredPayload= ((payload [-1 to 1] map (item1, index1) ->
{
(if (item1.value as Number != 0) (item1) else null)
}) filter ($ != {}))
// get the totalValue from the filteredPayload
var totalFilteredPayload = filteredPayload reduce ($.value $$.value)
---
// simply add both the arrays
filteredPayload [{ "valueTotal": totalFilteredPayload as String }]
CodePudding user response:
If by "zeroed out" you mean value
= 0 it is just a basic filter
operation
payload filter ($.valueTotal? or ($.value as Number != 0))
The condition $.valueTotal?
is to make the object with valueTotal
pass the check. And the other one is for the value
itself.