DataWeave: Common and Uncommon Elements in Array
Sep 29, 2022
How to find out the common and uncommon elements between two arrays.
Uncommon Elements
DataWeave Logic
//How to filter out the uncommon elements between two arrays.
%dw 2.0
output application/json
var array1 = [ "a1", "b1", "b2" ]
var array2 = [ "a1", "a2" ]
//a3 variable will be used only for thirdSolution
var a3 = array1 filter (array2 contains $)
---
{
firstSolution: array1 filter !(array2 contains $) ++ (array2 filter !(array1 contains $)),
secondSolution: (array1 -- array2) ++ (array2 -- array1),
thirdSolution: flatten(array1 + array2) distinctBy ((item, index) -> item) -- a3 ,
fourthSolution: flatten(((((array1 ++ array2) groupBy $) pluck $ ) filter sizeOf($) < 2))
}
Common Elements
DataWeave Logic:
%dw 2.0
output application/json
var a = [2,3,4,5,6,8]
var b = [1,2,4,6,8,9]
---
{
firstSolution: a filter (b contains $),
secondSolution: a -- (a -- b)
}