Merge two arrays and remove duplicates in MuleSoft
There are two main ways to achieve merging arrays and removing duplicates in MuleSoft 4 using DataWeave:
Method 1: Using ++ (concatenation) and distinctBy
This method is straightforward and efficient. Here's the DataWeave code:
%dw 2.0
output application/json
var arr1 = [1, 2, 3, 4];
var arr2 = [3, 4, 5, 6];
// Concatenate the arrays
var merged = arr1 ++ arr2;
// Remove duplicates based on the value itself
var distinct = merged distinctBy $;
---
// Output: [1, 2, 3, 4, 5, 6]
Explanation:
We define two sample arrays arr1 and arr2.
The ++ operator concatenates these arrays into a new variable merged.
The distinctBy $ function removes duplicates from merged. It uses the element itself ($) as the criteria for uniqueness.
Method 2: Using map and filter
This method involves iterating through the arrays and checking for duplicates. It's slightly less efficient but offers more flexibility for conditional filtering.
%dw 2.0
output application/json
var arr1 = [1, 2, 3, 4];
var arr2 = [3, 4, 5, 6];
// Combine arrays with filtering
var allElements = arr1 map $ ++ arr2 filter not (arr1 contains $);
---
// Output: [1, 2, 3, 4, 5, 6]
Explanation:
We define two sample arrays again.
The map $ expression iterates through arr1 and creates a new array with each element.
We concatenate (++) this result with arr2.
The filter function removes elements from the combined array that already exist in arr1 using the contains operator.
Choosing the Right Method:
For a simple merge and de-duplication scenario, Method 1 with ++ and distinctBy is recommended for efficiency.
If you need to filter based on additional criteria beyond just duplicates, Method 2 with map and filter provides more control.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.