Mulesoft Reduce Function Examples

The reduce function in mulesoft is used to perform reduction operations like finding the sum of all values in the array, multiplying the array values, concatenating the strings in the array, and many other operations.

The syntax of the reduce function is

inputArray reduce (item, acc) -> item + acc

Here item holds the current value of the array and acc is the accumulator that holds the result of the reduce operation. An initial value can be set to the accumulator.

In this article, we will see the usage of the reduce function with some examples.

Finding the Sum of array values

The sum function can be used to find the sum of array values. An example is shown below on the usage of the sum function.

%dw 2.0
output application/json
---
sum([1,2,3]) 

Now we will see how to find the sum of values in an array using the reduce function. This is shown in the following code.

%dw 2.0
output application/json
---
[1,2,3] reduce (item, acc) -> item + acc

Both the sum and reduce functions will produce the result 6.

Multiply the Values of Array

In the following DW code, the reduce operator multiplies all the values in the array. The initial value of the accumulator is set to 1.

%dw 2.0
output application/json
---
[4,5,6] reduce (item, acc = 1) -> item * acc

The execution of this DW code results in 120.

Concatenation of array values

The reduce function does not stop at doing math operations. It can be used to perform operations on strings as well. In the following example code, the reduce function concatenates all the values in the array.

%dw 2.0
output application/json
---
["a", "b", "c"] reduce (item, acc = "") -> acc ++ item

Here the accumulator is set to an empty string and the reduce function produces the concatenated string "abc".

Reverse the string

The reduce function not only performs an operation on the array, but it can also perform a reduction operation on the strings as well. In the below example, the reduce function reverses the input string.

%dw 2.0
output application/json
---
"mulesoft" reduce(item, acc="") -> item ++ acc

Here the reduce function iteratively reads each token of the string and concatenates in reverse order. This produces the output "tfoselum"

Reduce function on Array of Objects

The reduce function can operate on an array of objects as well. In the below example, the JSON contains objects of alphabets which the reduce function concatenates into a single string.

%dw 2.0
output application/json
---
[{"alpha" : "m"}, {"alpha" : "u"}, {"alpha" : "l"}, {"alpha" : "e"}] reduce(item, acc="") -> acc ++ item.alpha

Here the reduce operator takes each alpha object and concatenates each letter and produces the final result "mule".

Min and Max value in an Array

Interestingly, we can use the reduce function to find the min and max values in an array. We need to write if else logic in the lambda function to find the min and max values. This is shown in the following example.

%dw 2.0
output application/json
var v = [2,4,7,1,6]
---
{
  min : v reduce (item, acc = null) -> if(acc == null) item else if (acc < item) acc else item,
  max : v reduce (item, acc = null) -> if(acc == null) item else if (acc > item) acc else item,
}

The execution of this DW code results in the following output.

{
  "min": 1,
  "max": 7
}

Convert JSON Array to Object

This is a quite an interesting problem to solve. The reduce operator can be used to convert JSON array to Object. An example of the DW code is shown below.

%dw 2.0
output application/json
---
([0, 1, 2, 3, 4] reduce ((item, acc = {}) -> acc ++ { (item): item })) 

Here, you can see the accumulator variable is set to the empty object {} and it keeps accumulating each array value into the object. The final object is shown below:

{
  "0": 0,
  "1": 1,
  "2": 2,
  "3": 3,
  "4": 4
}

As you can see, we have solved many problems with the reduce function in mulesoft. There are many other real-time problems that you can encounter in your software projects. The usage of reduce operator can also improve the performance when applying reduction operation.