The reduce() function in DataWeave 1 and 2 is a function that helps us able to manipulate the elements of an array and output the type of data that 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:
1 |
<input> reduce (item, acc) -> <...> |
In which, <input> is the array that we will manipulate. The reduce() function will loop through all the items of the array, and for each item, it will perform the operation defined in the body of the Lambda Expression. The value of the acc parameter, if we don’t define an initial value for it, its value will start from the value of the first element of the array and we can define the return data type using this acc parameter, for example as follows:
1 |
acc = {} |
for Object object,
1 |
acc = [] |
for array and
1 |
acc = "" |
for String.
For example, I have the following array:
1 |
var arr = [1, 2, 3, 4] |
If we do nothing:
1 2 3 4 5 6 7 8 9 |
%dw 2.0 output application/json var arr = [1, 2, 3, 4] --- { item: arr reduce ((item, acc) -> item), acc: arr reduce((item, acc) -> acc) } |
then the last value of the item parameter will be the value of the last element in the array, and the last value of the acc parameter will be the value of the first element in the array.
If you want to sum, concatenate, reverse the string, you can write the code as follows:
1 2 3 4 5 6 7 8 9 10 |
%dw 2.0 output application/json var arr = [1, 2, 3, 4] --- { sum: arr reduce ((item, acc) -> acc + item), concat: arr reduce((item, acc) -> acc ++ item), reverse: arr reduce((item, acc) -> item ++ acc) } |
Result:
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:
1 2 3 4 5 6 7 8 |
%dw 2.0 output application/json var arr = [1, 2, 3, 4] --- { sum: arr reduce ((item, acc = 2) -> acc + item) } |
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:
If you have an object similar to the following:
1 2 3 4 |
var students = [ { "name": "Khanh", "age": 32 }, { "name": "Quan", "age": 25 } ] |
and you want the output to be an array of student names, you can code the following:
1 2 3 4 5 6 7 8 9 10 11 |
%dw 2.0 output application/json var students = [ { "name": "Khanh", "age": 32 }, { "name": "Quan", "age": 25 } ] --- { names: students reduce ((item, acc=[]) -> acc + item) } |
Here, we are defining the return data type for the acc parameter as an array, so the output will see the following result:
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:
1 2 3 4 5 6 7 8 |
%dw 2.0 output application/json var arr = [1, 2, 3, 4] --- { sum: arr reduce ($$ + $) } |
The result is still the same:
Sri
To get the number of elements of this array, and the first value of the acc parameter, you can code the following:
%dw 2.0
output application/json
var arr = [1, 2, 3, 4]
—
{
item: arr reduce ((item, acc) -> item),
acc: arr reduce((item, acc) -> acc)
}
This seems incorrect for me , because item always produces last value not the number of elements
Article is helpful. Thanks
Khanh Nguyen
Thank you, corrected.