0

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"
            }]
1
  • What is the issue with your script? What is exactly the objective? Commented Apr 13, 2022 at 11:47

1 Answer 1

2

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))) 
Sign up to request clarification or add additional context in comments.

4 Comments

and if I had to filter with 2 conditions or more ? I'm not able to achieve it.
@CarLoOSX please open a question with the details.
Sorry, at the end I found the way to do it : fun noMatchingFromTo(array1, array2) = array1 filter (item, index) -> not ((array2.*name contains ( item.name)) and (array2.*"type" contains ( item."type"))). Thank you :)
Again that should have gone into its own question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.