Contact Us 1-800-596-4880

reduce

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

reduce(Array<T>, (element: T, accumulator: T) -> T): T | Null

For each element of the input array, in order, reduce applies the reduction lambda expression (function), then replaces the accumulator with the new result. The lambda expression can use both the current input array element and the current accumulator value.

Note: If the array is empty and no default value is set on the accumulator, a null value is returned.

Parameters

Name Description

element

The current element from the array. Can also be referenced as $ if this parameter is not named in the lambda expression.

acc

The accumulator. Can also be referenced as $$. Used to store the result of the lambda expression after each iteration of the reduce operation.

The accumulator parameter can be set to an intial value using the syntax acc = initValue. In this case, the lambda expression is called with the first element of the input array. Then the result is set as the new accumulator value.

If an initial value for the accumulator is not set, the accumulator is set to the first element of the input array. Then the lamba expression is called with the second element of the input array.

The initial value of the accumulator and the lambda expression dictate the type of result produced by the reduce function. If the accumulator is set to acc = {}, the result usually is of type Object. If the accumulator is set to acc = [], the result usually is of type Array. If the accumulator is set to acc = "", the result is usually be a String.

Example

This example returns the sum of the values in the input arrays.

Source

%dw 2.0
output application/json
---
 {
    "sum" : [0, 1, 2, 3, 4, 5] reduce ($$ + $),
    "sum" : [0, 1, 2, 3, 4, 5] reduce ((elt, acc) -> acc + elt)
 }

Output

{
  "sum": 15,
  "sum": 15
}

Example

This example uses the accumulator to concatenate elements in the input arrays and return the results in a string.

Source

%dw 2.0
output application/json
---
{
   "concat" : ["a", "b", "c", "d"] reduce ($$ ++ $),
   "concat" : ["a", "b", "c", "d"] reduce ((elt, acc) -> acc ++ elt)
}

Output

{
  "concat": "abcd",
  "concat": "abcd"
}

Example

This example sets the first elements of the arrays to "z" and 3.

Source

%dw 2.0
output application/json
---
{
   "concat" : ["a", "b", "c", "d"] reduce ((elt, acc = "z") -> acc ++ elt),
   "sum": [0, 1, 2, 3, 4, 5] reduce ((elt, acc = 3) -> acc + elt)
}

Output

{
  "concat": "zabcd"
  "sum": 18
}

Example

This example shows a variety of uses of reduce, including its application to arrays of boolean values and objects.

Source

%dw 2.0
output application/json
var in0 =
{
  "a": [0, 1, 2, 3, 4, 5],
  "b": ["a", "b", "c", "d", "e"],
  "c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }],
  "d": [true, false, false, true, true]
}
---
{
  "a" : [0, 1, 2, 3, 4, 5] reduce $$,
  "b": ["a", "b", "c", "d", "e"] reduce $$,
  "c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }] reduce ((elt, acc = "z") -> acc ++ elt.letter),
  "d": [{ letter: "a" }, { letter: "b" }, { letter: "c" }] reduce $$,
  "e": [true, false, false, true, true] reduce ($$ and $),
  "f": [true, false, false, true, true] reduce ((elt, acc) -> acc and elt),
  "g": [true, false, false, true, true] reduce ((elt, acc = false) -> acc and elt),
  "h": [true, false, false, true, true] reduce $$,
  "i": in0.a reduce ($$ + $),
  "j": in0.a reduce ((elt, acc) -> acc + elt),
  "k": in0.a reduce ((elt, acc = 3) -> acc + elt),
  "l": in0.a reduce $$,
  "m": in0.b reduce ($$ ++ $),
  "n": in0.b reduce ((elt, acc) -> acc ++ elt),
  "o": in0.b reduce ((elt, acc = "z") -> acc ++ elt),
  "p": in0.b reduce $$,
  "q": in0.c reduce ((elt, acc = "z") -> acc ++ elt.letter),
  "r": in0.c reduce $$,
  "s": in0.d reduce ($$ and $),
  "t": in0.d reduce ((elt, acc) -> acc and elt),
  "u": in0.d reduce ((elt, acc = false) -> acc and elt),
  "v": in0.d reduce $$,
  "w": ([0, 1, 2, 3, 4] reduce ((elt, acc = {}) -> acc ++ { a: elt })) pluck $,
  "x": [] reduce $$,
  "y": [] reduce ((elt,acc = 0) -> acc + elt)
}

Output

{
  "a": 0,
  "b": "a",
  "c": "zabc",
  "d": { "letter": "a" },
  "e": false,
  "f": false,
  "g": false,
  "h": true,
  "i": 15,
  "j": 15,
  "k": 18,
  "l": 0,
  "m": "abcde",
  "n": "abcde",
  "o": "zabcde",
  "p": "a",
  "q": "zabc",
  "r": { "letter": "a" },
  "s": false,
  "t": false,
  "u": false,
  "v": true,
  "w": [ 0, 1, 2, 3, 4 ],
  "x": null,
  "y": 0
}

reduce(Array<T>, (item: T, accumulator: A) -> A): A