I want to remove all the objects from the users array coming in remove array. Please correct my DataWeave code mentioned below. Expected output is also mentioned.
%dw 2.0
output application/json
var remove =[
{
"startDate": "2022-04-13",
"objectName": "Account"
}
]
var users = [{
"startDate": 20220412,
"objectName": "Account"
},
{
"startDate": 20220412,
"objectName": "Blanket Agreemt"
}
]
---
users filter ((item, index) -> item.objectName != value.objectName) map (value, key) -> {
"objectName": value.objectName
}
Expected output:
[{
"startDate": 20220412,
"objectName": "Blanket Agreemt"
}]
The filter is invalid and the issue unclear. I'll do an educated guess that the objective is to return all elements from the list which their objectName is also present in one of the objects in remove
. The map() at the end doesn't seems to be needed.
To resolve we can collect all objectNames into a list with the multi-value selector (remove.*objectName
) and use contains() function to check if the current element of users
exists in the remove
array. Finally the not()
is needed to complete the condition for the filter
.
%dw 2.0
output application/json
var remove =[
{
"startDate": "2022-04-13",
"objectName": "Account"
}
]
var users = [
{
"startDate": 20220412,
"objectName": "Account"
},
{
"startDate": 20220412,
"objectName": "Blanket Agreemt"
}
]
---
users
filter ((item, index) -> not (remove.*objectName contains ( item.objectName)))