I have a payload that is an array of objects. I want to remove duplicate objects with the same id however if the returnComment is 1
, that should be kept over the one being 2
. Basically 1
has higher priority over 2
in deciding which object to keep. How would one do it based on this condition?
Payload:
[
{
"id": "1676431",
"returnComment": 2
},
{
"id": "1676431",
"returnComment": 1
},
{
"id": "1676431",
"returnComment": 2
},
{
"id": "3566367",
"returnComment": 2
},
{
"id": "7676559",
"returnComment": 1
},
{
"id": "7676559",
"returnComment": 1
}
]
Expected Output:
[
{
"id": "1676431",
"returnComment": 1
},
{
"id": "3566367",
"returnComment": 2
},
{
"id": "7676559",
"returnComment": 1
},
]
Current Code:
%dw 2.0
output application/json
---
arr distinctBy $.requestId