Search code examples
dataweavemulesoftmule4

How to compare two arrays and get only the common value in Mule 4 - Dataweave


I want to compare two arrays and get only the common values in Mule 4 dataweave

Input:

 array1 = ["aaa", "bbb"]
 array2 = ["aaa","ccc","ddd"]

Output:

 Result: ["aaa"]

I tried "--", diff but both didn't gave me expected result.

Thanks in advance


Solution

  • %dw 2.0
    var array1 = ["aaa", "bbb"]
    var array2 = ["aaa","ccc","ddd"]
    output application/java
    ---
    array1 reduce (item, acc = []) -> if (array2 contains item) acc + item else acc