how to check if any one element of a variable length String array is present in another variable length string array

Viewed 190
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.

2 Answers

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"]
%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
Related