Mulesy
MuleSoft Learning Made Easy &
REDUCE FUNCTION
" # Functions & Operators $
dataweave, reduce
REDUCE FUNCTION
Reduce function is used to do any computation
while iterating on an array, computation result
is not lost while iteration. it’s a very helpful
function where you have to do computation on
some array element.
reduce(Array<T>, (item: T, accumulator:
T) -> T): T | Null
or
Array<T> reduce( (item: T, accumulator:
T) -> T): T | Null
Here
Array – is the array on which we
want to apply reduce function
Item
item in the input array
Refer as $
Takes 1st array item
value in case acc is
defined
Takes 2nd array item
value in case acc is not
defined
Accumulator (acc)
Store the results of
lambda expression after
each iteration in reduce
function
Refer as $$
We can explicitly define
the initial value like
[2,3,3] reduce ((item,
acc = “2”) → acc * item)
If initial value is not
defined then it will take
the value of first item of
array.
Iteration
no of item in array
minus 1 in case acc is
not initialized
no of item in array in
case acc is initialized
Lets see the use case where
acc value is not initialized
Dataweave:
%dw 2.0
output application/json
---
[2, 3] reduce ($ + $$)
Here
[2,3] – is the input array
acc -> $$ will take the 1st item value
= 2 (as acc is not initialized)
$ will take the 2nd item value = 3 (as
acc is not initialized)
Loop count = no of item in array
minus 1 (as acc is not initialized) = 2
–1=1
Acc = ($=3 + $$=2) = 5
So result will be 5
acc value is initialized
Dataweave:
%dw 2.0
output application/json
---
[2,3] reduce ((item, acc = 4) -> acc +
item)
Here
[2,3] – is the input array
acc will take the initialized value = 4
item will take 1st item value = 2 (as
acc is initialized)
Loop count = no of item in array (as
acc is initialized) = 2
Acc = acc + item -> 4 +
2 -> 6
Acc = acc + item -> 6 +
3 -> 9
So result will be 9
more details
Thank you for taking out time to read the above
post. Hope you found it useful. In case of any
questions, feel free to comment below. Also, if
you are keen on knowing about a specific topic,
happy to explore your recommendations as
well.
For any latest updates or posts on our website,
you can follow us on LinkedIn. Look forward to
connecting with you there.
Full Stack Course in Bangalore
100% Advance Practical
Training with Lifetime LMS,
10+ Certi=cation, Job
Guarantee & More
Ad digitalacademy360.com Visit Site
Descendants Usage of filterObject
Selector In Dataweave
Share this:
Li C O T W S
n o u w h h
! Subscribe !
Join the discussion
{} [+] %
3 COMMENTS
" # Oldest $
Rizwan " 2 years ago % &
Hello Admin,
while concatenating using reduce function
getting the output as “54321”
—-
if acc value is not initialized
acc -> $$ will take the 1st item value
$ will take the 2nd item value
[1,2,3,4,5] reduce ((item,acc) -> item ++
4 Reply
suzi " 1 year ago % &
I/P
a = [1,2,3,4,5,6]
b = [3,4,5,6]
i want to print duplicates using reduce function
O/P
C = [3,4,5,6]
9 Reply '
Niranjan Koduru " 4 months ago % &
# Reply to suzi
[(a ++ b reduce ((item, acc) -> acc ++ item)
splitBy “”) distinctBy ($) joinBy “”]
$ Last edited 4 months ago by Niranjan Koduru
1 Reply
SEARCH MULESOFT TOPIC
Topic !
TRANSLATE
English
TOPICS
+ open all | - close all
Tutorials for Beginners
MuleSoft Installations
MuleSoft POCs
Access Management
Advance
Basic
Batch Jobs
CI-CD-Setup
Connectors
Anypoint MQ
AWS-Amazon Web Services
Azure Cosmos DB
Azure Storage
Cloudhub Connector
Compression Module
Compress and Decompress File In Mule 4
Create Zip file in Mule 4
Read Zip file in Mule 4
Custom Connector
Database
Dropbox
Email
File
Google Drive
HTTP Connector
Java
JMS
Kafka
LDAP
MicroSoft Service Bus
Neo4j
Object Store
PGP Crypto Connector
Salesforce
Scripting
ServiceNow
SFTP
Sharepoint
Solace
Twilio Connector
Validation Module
Web Service Consumer
Websocket
Core Components
Dataweave
Use Cases
Flow Control
Functions & Operators
Descendants Selector
Filter In Dataweave Mule 4
Retrieve Unique Records In Dataweave 2.0
Map function in Dataweave
Reduce Function
Sort Using OrderBy In Mule 4
Usage of filterObject In Dataweave
Dataweave Tips & Guidelines
Custom DWL Function Using Custom Module
Dataweave Streaming In Mule 4
Debug Dataweave in MuleSoft
External Jar Class in Dataweave
Invoke Custom Java Class Using Dataweave
Dataweave 2.3.0 New Features
Log Inside Dataweave
Online Dataweave Debugging
Play with brackets () {} [] and Complex If-else co
Read Properties In Mule 4
Variables In Dataweave
Deployment
Munits
RAML
Security
Swagger
MuleSoft Interview Questions
Anypoint Platform APIs
MuleSoft API Manager
Misc
Mulesoft Best Practices
MuleSoft Certifications
MuleSoft Integration Patterns
Mulesoft New Offerings
%
RECENT POSTS
Simulating VCores in Anypoint Studio
Connect with Google Drive
MuleSoft Anypoint Datagraph
Azure AD as Client Provider
Play with brackets () {} [] and Complex If-else
conditions
POPULAR POSTS
Convert Array To String In Dataweave 2.0
MuleSoft API lifecycle
JSON TO CSV Conversion In Mule 4
If Else In Dataweave
Map function in Dataweave
POPULAR TAGS
amazon web services API Manager aws Azure Container
in Mule Azure Storage Connector Cosmos Crypto
dataweave interview question mule mule 4
mulesoft MuleSoft Azure MuleSoft Integration Pattern
mulesoft interview questions MySQL object store PGP policy s3
salesforce
LINKS
LinkedIn
Testimonials / Submit Testimonial
Contributory
Suggestions
Email us – muleisy@gmail.com
© All right reserved 2022