2
inputArray = ["cat", "bat", "mat"]  
configuredArray = ["dog", "elephant", "fox", "cat"]

inputArray and configuredArray are variable length String arrays.

If any one element of the inputArray is present in the configuredArray I would like to set a bloolean flag. How do I write it in Dataweave 2.0? Thanks in advance.

3 Answers 3

7

You could leverage the filter and contains functions and do something like this. Also leaves you with a reusable functin.

%dw 2.0
output application/json

fun any(left: Array, right: Array) =
    sizeOf(left filter (right contains $)) > 0

---
["cat", "bat", "mat"] any ["dog", "elephant", "fox", "cat"]
Sign up to request clarification or add additional context in comments.

3 Comments

extra point for using a binary function as an operator.
Great and concise solution
Keep in mind that this method has no short-circuit logic, so for especially large payloads this isn't going to be the fastest.
0
%dw 2.0
output application/json
var arr1 = ["cat", "bat", "mat"]
var arr2 = ["dog", "elephant", "fox", "cat"]

---
sizeOf(arr1 reduce (item, acc = []) -> if (arr2 contains item) acc + item else acc) >0

Comments

0

Perform -- operation between the arrays and based on difference in size, we can easily evaluate it.

%dw 2.0
output application/json
var inputArray = ["cat", "bat", "mat"]  
var configuredArray = ["dog", "elephant", "fox", "cat"]
---
{
   match : (sizeOf(inputArray -- configuredArray) < sizeOf(inputArray))
}

Comments

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.