Reduce Function in DataWeave

Reduce is a useful operator that applies a reduction expression on an array. This function can operate on each of the elements in an array. Syntax: Array reduce( (item: T, accumulator: T) -> T): T | Null OR reduce(Array, (item: T, accumulator: T) -> T): T | Null Here, Array is an array on which… Continue reading Reduce Function in DataWeave
Reduce is a useful operator that applies a reduction expression on an array. This function can operate on each of the elements in an array.
Syntax:
Array<T> reduce( (item: T, accumulator: T) -> T): T | Null
OR
reduce(Array<T>, (item: T, accumulator: T) -> T): T | Null
Here,
Array
is an array on which we want to apply reduce function.
Item
In the input array, this is an item.
Refer to it as $.
If acc is defined, it takes the first array item value.
If acc isn’t defined, it uses the second array item value.
Accumulator (acc)
Reduce function saves the results after each iteration.
Refer to it as $$.
The initial value can be written as [0, 1, 2, 3 ,4, 5] reduce ((item, acc = 2) → acc * item).
Refer to it as $$.
If no starting value is specified, then in this case the value of the first item of array will be used.
Iteration
Number of item in array minus 1 in case acc is not initialized.
Number of item in array in case acc is initialized.
Syntax Example:
Input :
%dw 2.0 output application/json --- { "method1": [0, 1, 2, 3, 4, 5] reduce (item, acc) -> item + acc, "method2": reduce ([0, 1, 2, 3, 4, 5], (item, acc) -> item + acc) }
Output:
{ "method1": 15, "method2": 15 }
Let’s see the use case when
- Accessing array items through ‘$’ and ‘$$’: In this we’ll be using the reduce function with the ‘$’ and ‘$$’.
Where,
‘$’ represents the current value of the iteration.
‘$$’ represents the aggregation of the result which could be anything i.e., a Number, an Object, an Array etc.
Input:
%dw 2.0 output application/json var myArr = [0, 1, 2, 3, 4, 5] --- { "Addition": myArr reduce $ + $$, "Subtraction": myArr reduce $ - $$, "Multiplication": myArr reduce $ * $$, }
Output:-
{ "Addition": 15, "Subtraction": 3, "Multiplication": 0 }
- Accessing array items through ‘item’ and ‘accumulator’:
Where,
‘item’ represents the current value of the iteration.
‘accumulator’ represents the aggregation of the result which could be anything i.e., a Number, an Object, an Array etc.
Input:
%dw 2.0 output application/json var myArr = [0, 1, 2, 3, 4, 5] --- { "Addition": myArr reduce (item, acc) -> item + acc, "Subtraction": myArr reduce (item, acc) -> item - acc, "Multiplication": myArr reduce (item, acc) -> item * acc, }
Output:
{ "Addition": 15, "Subtraction": 3, "Multiplication": 0 }
- Accumulator (acc) value is not initialized: Here we did not assign any value to the accumulator(acc) so it will take the 0th index value of an array and item will equal to the 1st index of an array.
For example: In the below mention code, for first iteration the acc = myArr[0], and an item = myArr[1]. i.e., acc = 1 and item = 2.
Input:
%dw 2.0 output application/json var myArr = [1, 2, 3, 4, 5] --- { "Addition": myArr reduce (item, acc) -> acc + item, "Subtraction": myArr reduce (item, acc) -> acc - item, "Multiplication": myArr reduce (item, acc) -> acc * item, "Divide": myArr reduce (item, acc) -> acc / item }
Output :
{ "Addition": 15, "Subtraction": -13, "Multiplication": 120, "Divide": 0.008333333333333333333333333333333336 }
- Accumulator(acc) value is initialized: Here we assign the value of accumulator with 2 i.e., now the acc = 2 and the item will be equal to the 0th index of an array.
For example: In the below mention code, for first iteration an item = myArr[0]. i.e., acc = 2 and item = 1.
Input:
%dw 2.0 output application/json var myArr = [1, 2, 3, 4, 5] --- { "Addition": myArr reduce (item, acc=2) -> acc + item, "Subtraction": myArr reduce (item, acc=2) -> acc - item, "Multiplication": myArr reduce (item, acc=2) -> acc * item, "Divide": myArr reduce (item, acc=2) -> acc / item }
Output:
{ "Addition": 17, "Subtraction": -13, "Multiplication": 240, "Divide": 0.01666666666666666666666666666666666 }
- + and ++ in reduce function:
Input:
%dw 2.0 output application/json var myArr = [0, 1, 2, 3, 4, 5] --- { "sum": myArr reduce ($$ + $), "concat": myArr reduce ($$ ++ $) }
Output:
{ "sum": 15, "concat": "012345" }
- When the array’s items are in bollean form: Here all the items of an array is in Bollean form.
Input:
%dw 2.0 output application/json var myBolleanArr = [true, true, false, true, true] --- { "AND operator": myBolleanArr reduce ($$ and $), "OR operator": myBolleanArr reduce ($$ or $) }
Output:
{ "AND operator": false, "OR operator": true }
- To fetch first and last item from an array: We can fetch the first item of an element by returning the ‘$’ and last item by return the ‘$$’.
Input:
%dw 2.0 output application/json var myBolleanArr = [true, true, false, true, false] --- { "last_item_of_array": myBolleanArr reduce $, "first_item_of_array": myBolleanArr reduce $$, }
Output:
{ "last_item_of_array": false, "first_item_of_array": true }
- Fold Left and Fold Right in Reduce function
Fold Left: In this case, we mention the accumulator to the left side of an item.
Input:
%dw 2.0 output application/json var myArr = [1, 2, 3, 4, 5] --- { "Addition": myArr reduce (item, acc) -> acc + item, "Subtraction": myArr reduce (item, acc) -> acc - item, "Multiplication": myArr reduce (item, acc) -> acc * item, "Divide" : myArr reduce (item, acc) -> acc / item }
Output:
{ "Addition": 15, "Subtraction": -13, "Multiplication": 120, "Divide": 0.008333333333333333333333333333333336 }
Fold Right: In this case, we mention the accumulator to the right side of an item.
Input:
%dw 2.0 output application/json var myArr = [1, 2, 3, 4, 5] --- { "Addition": myArr reduce (item, acc) -> item + acc, "Subtraction": myArr reduce (item, acc) -> item - acc, "Multiplication": myArr reduce (item, acc) -> item * acc, "Divide" : myArr reduce (item, acc) -> item / acc }
Output:
{ "Addition": 15, "Subtraction": 3, "Multiplication": 120, "Divide": 1.875000000000000000000000000000000 }
Here the result for ‘Addition’ and ‘Multiplication’ will always be same for both the cases but vary for the ‘Subtraction’ and ‘Divide’.
Thanks for reading and please add your comments in the section below. Find more Mule best practices by Caelius Consulting at Resource Centre.
Recent Blogs

Connecting MuleSoft and Azure SQL with Entra ID
Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID
Connecting MuleSoft and Azure SQL with Entra ID
Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Understanding Salesforce Flow Approval Processes
Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes
Understanding Salesforce Flow Approval Processes
Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes

Capturing Real-time Record Updation Using LWC
Introduction In modern CRM ecosystems, real-time Salesforce integration and seamless user experiences are no longer optional but fundamental for driving operational efficiency. Imagine your sales reps making important Opportunity changes, but the ERP remains out of sync, leading to confusion and data errors. We understood the necessity to bridge this data gap and implemented a… Continue reading Capturing Real-time Record Updation Using LWC
Capturing Real-time Record Updation Using LWC
Introduction In modern CRM ecosystems, real-time Salesforce integration and seamless user experiences are no longer optional but fundamental for driving operational efficiency. Imagine your sales reps making important Opportunity changes, but the ERP remains out of sync, leading to confusion and data errors. We understood the necessity to bridge this data gap and implemented a… Continue reading Capturing Real-time Record Updation Using LWC

All About Schedulers: Mule 4
In the world of Mule 4, automating repetitive tasks and triggering flows at defined intervals is necessary for building efficient and robust integration solutions. This is where Mule 4 schedulers come into use. This blog post explores the intricacies of scheduling in Mule 4, providing practical examples and best practices to help you get deeper… Continue reading All About Schedulers: Mule 4
All About Schedulers: Mule 4
In the world of Mule 4, automating repetitive tasks and triggering flows at defined intervals is necessary for building efficient and robust integration solutions. This is where Mule 4 schedulers come into use. This blog post explores the intricacies of scheduling in Mule 4, providing practical examples and best practices to help you get deeper… Continue reading All About Schedulers: Mule 4