Study guide
8.2 Core Functions: map, filter, pluck, reduce, groupBy & orderBy
Salesforce Certified MuleSoft Developer I Exam
Track your progress as you read
Finished sections get a check mark.
1 Chapter 1: Creating Application Networks & API-Led Connectivity
1.1 The IT Delivery Gap & API-Led Connectivity Architecture 1.2 Application Network Foundations & Center for Enablement (C4E) 1.3 Anypoint Platform Architecture & Core Components
2 Chapter 2: Designing APIs with RAML & API Designer
2.1 RAML 1.0 Fundamentals, Data Types & Resource Hierarchy 2.2 Reusable API Fragments, ResourceTypes & Traits 2.3 API Mocking Service, Documentation & Anypoint Exchange Publishing
3 Chapter 3: Accessing and Modifying Mule Events
3.1 Mule 4 Event Architecture: Message, Payload, Attributes & Variables 3.2 Flow Variable Management & Target Variables Pattern 3.3 Accessing Event Metadata, Inbound Attributes & Context Properties
4 Chapter 4: Structuring Mule Applications
4.1 Modular Flow Composition: Flows, Subflows & Private Flows 4.2 Configuration Property Files & Environment Management 4.3 Secure Properties Tool, Encryption & Maven Project Packaging
5 Chapter 5: Building API Implementation Interfaces
5.1 APIkit Scaffolding, Router Configuration & Interface Generation 5.2 HTTP Listener Configuration, Path Matching & Query/URI Parameters 5.3 Interface vs. Implementation Separation & Backend Flow Wiring
6 Chapter 6: Using Connectors & External Systems
6.1 Database Connector: Select, Insert, Update, Stored Procedures & Watermarking 6.2 HTTP Request Connector: Headers, Query Params, Response Timeouts & Target Variables 6.3 Salesforce Connector, JMS Messaging & File/FTP Connectors
7 Chapter 7: Processing Records & Batch Integration
7.1 For Each Scope: Payload Iteration, Batch Size & Streaming Data 7.2 Batch Job Architecture: Input, Process, and Complete Phases 7.3 Batch Step Accept Expression, Failure Handling & Batch Aggregator 7.4 Object Store: Persisting Data Between Flow Executions
8 Chapter 8: Transforming Data with DataWeave 2.0
8.1 DataWeave 2.0 Syntax, Type System & Selectors 8.2 Core Functions: map, filter, pluck, reduce, groupBy & orderBy 8.3 Custom Functions, Format Transformations & Type Coercion 8.4 Calling Mule Flows from DataWeave & Reusing DataWeave Modules
9 Chapter 9: Routing Events & Flow Control
9.1 Choice Router: Conditional Expressions & Default Routing 9.2 Scatter-Gather Router: Parallel Execution, Aggregation & Error Traps 9.3 Async Scope, Round Robin & First Successful Routing 9.4 Validating Mule Events with the Validation Module
10 Chapter 10: Handling Errors & Exception Management
10.1 Mule 4 Error Object Hierarchy & Custom Error Types 10.2 On-Error Continue vs. On-Error Propagate Mechanisms 10.3 Try Scope Exception Isolation & Global Error Handling Strategies
11 Chapter 11: Debugging and Troubleshooting Mule Applications
11.1 Logger Configuration, Categories & Thread/Context Diagnostics 11.2 Anypoint Studio Debugger: Breakpoints, Expression Watches & Step-Through 11.3 MUnit 2.x Testing: Unit Tests, Mocking Connectors & Assertions
12 Chapter 12: Deploying and Managing APIs and Integrations
12.1 CloudHub 1.0 & 2.0 Architecture: Workers, vCores & Clustering 12.2 Runtime Manager: Application Management, Alerts, Insights & Logs 12.3 API Manager: Auto-Discovery, API Gateway, SLA Tiers & Standard Policies
8.2 Core Functions: map, filter, pluck, reduce, groupBy & orderBy
Key Takeaways Explain This Topic AI
The map function iterates over an Array to transform each element, exposing parameters (item, index) or anonymous positional indicators $ (current value) and $$ (current index), returning a new Array of identical length.
The mapObject function iterates over Key-Value pairs in an Object, exposing (value, key, index) or $ (value), $$ (key name), and $$$ (index), returning a new transformed Object. Key expressions must be wrapped in parentheses ((key): value) when dynamically generated.
The filter function evaluates a boolean predicate against an Array, keeping only elements returning true, while filterObject filters Key-Value pairs of an Object.
The pluck function transforms an Object into an Array by extracting keys, values, or indices, bridging the gap between key-value maps and iterable list collections.
The reduce function aggregates an Array into a single accumulated result, groupBy partitions an Array into an Object of Arrays keyed by grouping criteria, and orderBy/distinctBy sort and deduplicate collections.
Last updated: August 2026
Core Functions: map, filter, pluck, reduce, groupBy & orderBy
Data transformations in enterprise integration rarely involve simple scalar values; they predominantly center around manipulating collections of records—such as arrays of database rows, lists of JSON objects, and key-value maps. DataWeave provides a suite of higher-order functional operations for transforming, filtering, aggregating, and reshaping collections cleanly and declaratively.
1. Transforming Arrays with map
The map function iterates over each element of an Array , applies a transformation expression, and returns a new Array containing the transformed elements in corresponding order. +-----------------------------------------------------------------------------------------+
- | ARRAY MAP OPERATION |
- | |
- | Input Array: [ { id: 1, name: "A" }, { id: 2, name: "B" } ] |
- | |
- | Transformation: payload map ((item, index) -> { ... }) |
- | | |
- | +---> Item 0: item = { id: 1, name: "A" }, index = 0 |
- | | Output 0: { customerId: 1, displayName: "A", seq: 1 } |
- | | |
- | +---> Item 1: item = { id: 2, name: "B" }, index = 1 |
- | Output 1: { customerId: 2, displayName: "B", seq: 2 } |
- | |
- | Output Array: [ { customerId: 1, displayName: "A", seq: 1 }, |
- | { customerId: 2, displayName: "B", seq: 2 } ] |
- +-----------------------------------------------------------------------------------------+
- Explicit vs. Shorthand Syntax
DataWeave supports explicit lambda parameter definitions as well as anonymous positional parameters:
Parameter Style Value Identifier Index Identifier Example Syntax
Explicit Parameters item (or custom name) index (or custom name) payload map ((item, index) -> { id: item.id, pos: index })
- Anonymous / Shorthand $ (Current element) $$ (Zero-based index) payload map ({ id: $.id, pos: $$ })
- Array Transformation Example: %dw 2.0
- output application/json
- var rawAccounts = [
- { "acc_no": 1001, "acc_name": "Acme Corp", "active": "Y" },
- { "acc_no": 1002, "acc_name": "Global Dynamics", "active": "N" }
- ]
- --
- rawAccounts map ((account, idx) -> {
- accountId: account.acc_no,
- accountName: upper(account.acc_name),
- isActive: account.active == "Y",
- sequenceNumber: idx + 1
- })
Behavior with Null Inputs:
If the input expression to map evaluates to null , DataWeave returns null rather than throwing an exception. For example, null map ($) safely evaluates to null .
2. Transforming Objects with mapObject
While map operates strictly on Arrays, mapObject iterates over the key-value pairs of an Object and returns a newly transformed Object . +-----------------------------------------------------------------------------------------+
- | OBJECT MAPOBJECT OPERATION |
- | |
- | Input Object: { "firstName": "John", "lastName": "Doe", "age": 30 } |
- | |
- | Transformation: payload mapObject ((val, key, idx) -> (upper(key)): val) |
- | | |
- | +---> Pair 0: val = "John", key = "firstName", idx = 0 |
- | +---> Pair 1: val = "Doe", key = "lastName", idx = 1 |
- | +---> Pair 2: val = 30, key = "age", idx = 2 |
- | |
- | Output Object: { "FIRSTNAME": "John", "LASTNAME": "Doe", "AGE": 30 } |
- +-----------------------------------------------------------------------------------------+
Anonymous Parameter Identifiers for mapObject :
$ : The field's Value .
$$ : The field's Key (as a Key type).
$$$ : The field's zero-based Index (Number).
The Dynamic Key Parentheses Rule
When constructing key-value pairs inside an object in DataWeave, if the key name is dynamic or generated from an expression/variable, it must be enclosed in parentheses (...) . %dw 2.0
- output application/json
- var user = {
- "first_name": "Alice",
- "last_name": "Smith",
- "email_address": "alice@example.com"
- }
- --
- user mapObject ((value, key, index) -> {
- // Dynamic key must use parentheses (key): value
- (upper(key)): value
- })
Important
Dynamic Key Parentheses Requirement In DataWeave object construction, { key: value } creates a literal key named "key" . To evaluate the identifier as an expression or variable, you must write { (key): value } . Omitting parentheses causes DataWeave to output literal key names rather than evaluated dynamic keys.
3. Filtering Collections: filter & filterObject
Filtering removes elements or key-value pairs that do not satisfy a specified boolean condition.
filter on Arrays
filter takes an array and a boolean predicate expression. It returns a new array containing only elements where the predicate evaluates to true : %dw 2.0
- output application/json
- var products = [
- { "id": 1, "name": "Chair", "price": 45, "inStock": true },
- { "id": 2, "name": "Desk", "price": 250, "inStock": false },
- { "id": 3, "name": "Monitor", "price": 300, "inStock": true }
- ]
- --
- // Shorthand filter using $ (current item)
- products filter ($.inStock and $.price >= 50)
- Output: [
- {
- "id": 3,
- "name": "Monitor",
- "price": 300,
- "inStock": true
- }
- ]
- filterObject on Objects
filterObject iterates over key-value pairs in an object and retains only those pairs that meet the criteria: %dw 2.0
- output application/json
- var rawCustomer = {
- "name": "Global Tech",
- "taxId": null,
- "email": "",
- "phone": "+1-555-0199",
- "notes": null
- }
- --
- // Strip null and empty string properties
- rawCustomer filterObject ((value, key) -> value != null and value != "")
- Output: {
- "name": "Global Tech",
- "phone": "+1-555-0199"
- }
- 4. Converting Objects to Arrays with pluck
DataWeave does not allow running map directly on an Object . When an integration receives a key-value map and needs to transform it into a JSON array, the pluck function is used. +-----------------------------------------------------------------------------------------+
- | OBJECT PLUCK OPERATION |
- | |
- | Input Object: |
- | { |
- | "USD": 1.00, |
- | "EUR": 0.92, |
- | "GBP": 0.78 |
- | } |
- | |
- | Transformation: payload pluck ((val, key, idx) -> { currency: key, rate: val }) |
- | |
- | Output Array: |
- | [ |
- | { "currency": "USD", "rate": 1.00 }, |
- | { "currency": "EUR", "rate": 0.92 }, |
- | { "currency": "GBP", "rate": 0.78 } |
- | ] |
- +-----------------------------------------------------------------------------------------+
Anonymous Parameter Identifiers for pluck :
$ : Value of the current key-value pair.
$$ : Key of the current key-value pair (as a Key type; cast to String using $$ as String ).
$$$ : Zero-based index of the entry.
- Practical pluck Example: %dw 2.0
- output application/json
- var errorDictionary = {
- "ERR_01": "Invalid credentials",
- "ERR_02": "Account locked",
- "ERR_03": "Session expired"
- }
- --
- errorDictionary pluck ((description, code, index) -> {
- code: code as String,
- message: description,
- priority: index + 1
- })
- 5. Aggregations & Grouping: reduce , groupBy , orderBy & distinctBy
- reduce : Folding Collections into a Single Result
- The reduce function iterates over an array and collapses it into a single cumulative output value. %dw 2.0
- output application/json
- var orderItems = [
- { "sku": "A1", "price": 25.0, "qty": 2 },
- { "sku": "B2", "price": 50.0, "qty": 1 },
- { "sku": "C3", "price": 10.0, "qty": 4 }
- ]
- --
- {
- // Explicit reduce with accumulator default
- totalCost: orderItems reduce ((item, accumulator = 0) ->
- accumulator + (item.price * item.qty)
- ),
- // Shorthand reduce over numbers (Note: $$ is accumulator, $ is item)
- simpleSum: [10, 20, 30, 40] reduce ($$ + $)
- }
Warning
Positional Parameters in reduce vs map In map , $ is the item and $$ is the index .
In reduce , $ is the current item and $$ is the accumulator (running total). This distinction is a frequent topic on the Developer I exam.
groupBy : Partitioning Arrays into Maps
groupBy groups elements of an array according to a key-generating expression, returning an Object where each key maps to an Array of matching records: %dw 2.0
- output application/json
- var employees = [
- { "name": "Alice", "dept": "Engineering", "salary": 95000 },
- { "name": "Bob", "dept": "Sales", "salary": 75000 },
- { "name": "Charlie", "dept": "Engineering", "salary": 110000 }
- ]
- --
- employees groupBy ($.dept)
- groupBy Output: {
- "Engineering": [
- { "name": "Alice", "dept": "Engineering", "salary": 95000 },
- { "name": "Charlie", "dept": "Engineering", "salary": 110000 }
- ],
- "Sales": [
- { "name": "Bob", "dept": "Sales", "salary": 75000 }
- ]
- }
- orderBy and distinctBy
orderBy : Sorts an array based on an extraction expression or list of criteria: payload orderBy ($.price) or payload orderBy [$.dept, -$.salary] (prefix - sorts descending).
distinctBy : Deduplicates an array by retaining only the first item that produces a unique value for the criteria: payload distinctBy ($.email) .
Collection Utility Matrix
Function Input Output Common Use Case
map Array<T> Array<R> Transform each element of an array to a target schema.
mapObject Object Object Transform keys and values of a key-value object.
filter Array<T> Array<T> Retain array elements that satisfy a condition.
filterObject Object Object Strip null, empty, or sensitive key-value pairs from an object.
pluck Object Array<R> Convert key-value maps into iterable array lists.
reduce Array<T> R (Scalar/Obj) Compute sums, averages, or accumulate single summary objects.
groupBy Array<T> Object<Array<T>> Bucket records by department, status, or category.
flatten Array<Array<T>> Array<T> Flatten nested two-dimensional arrays into a 1D array.
6. Exam Watch: Core Collection Scenarios
- Important
- Choosing Between map , mapObject , and pluck
If the input is an Array and you want an Array : use map .
If the input is an Object and you want an Object : use mapObject .
If the input is an Object and you want an Array : use pluck .
Tip
Combining groupBy and mapObject for Aggregations A standard enterprise pattern is grouping transactions by category with groupBy , then chaining mapObject and reduce to calculate category-level totals in a single transformation pipeline.