Using reduce() function in DataWeave

The reduce() function in DataWeave 1 and 2 is a function that helps us able to manipulate on the elements of an array and output to the type of data which we want.

To do this, the parameters of the reduce() function include the item in the array we are going to manipulate and the second parameter accumulator will store and define the type of data we want to return. The structure of the reduce() function is written in a Lambda Expression style from Java 8 as follows:

Inside, <input> is the array that we will manipulate. The reduce() function will loop through all of the array’s items, and for each item, it will perform the operation defined in the acc (accumulator) parameter. The acc parameter is not only defining the operation we want such as adding, subtracting, concatenating strings, … but aslo storing the value we manipulated with the elements in the array before the element it is processing. The value of the acc parameter will start from the first element of the array and we can define the return data type using this acc parameter as follows:

for Object object,

for array and

for String.

For example, I have the following array:

To get the number of elements of this array, and the first value of the acc parameter, you can code the following:

Result:

Using reduce() function in DataWeave

Because here, we do not perform anything, so as you can see, the default value of the parameter acc and the number of elements of the array will be printed.

If you want to sum, concatenate, reverse the string, you can write the code as follows:

Result:

Using reduce() function in DataWeave

The definition of the operation before or after the acc parameter plus defining the output in this parameter will help us get the desired result, DataWeave will rely on these definitions to return results.

By default, the value of the acc parameter will be the value of the first element in the array, but if you set it to the initial value, for example:

then the initial value of the parameter acc will be this value.

As the above example, I assigned the parameter acc an initial value of 2, so when calculating the sum of the elements of the array above, the final value will be is 12:

Using reduce() function in DataWeave

If you have an object similar to the following:

and you want the output to be an array of student names, you can code the following:

Here, we are defining the return data type for the acc parameter as an array, so the output will see the following result:

Using reduce() function in DataWeave

You can also use the $ character to replace the array’s item parameter and $$ instead of the acc parameter when using the reduce() function, similar to the pluck() function in DataWeave.

We can write code using $ and $$ as follows:

The result is still the same:

Using reduce() function in DataWeave

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment