Scenario : Using Reduce method in Mule 4 to reduce a LIST into three parameters :
- Students LIST
- Teachers List
- Number of Students
Using the below Dataweave Code in Transform Message :
%dw 2.0
output application/java
---
payload reduce((value, acc = { 'totalStudents': 0 as Number,'studentList' : [], 'teachersList' : []}) ->
if(
value.age > 18 and value.age < 25
){
totalStudents : (acc.totalStudents default 0 as Number) + 1,
studentList : (acc.studentList default [] ) << {
'studentName' : value.Name ++ " is a Student"
}
}else{
teachersList : acc.teachersList default [] << value.Name ++ " is a Teacher"
}
)
PROBLEM Statement: The transform message is processed successfully but in the payload I am getting only two values:
- payload.totalStudents and
- payload.studentList
Can anyone help me to understand why am I not getting payload.teachersList in my result?
Finally this should get what you are looking for: