---
schema_version: "1"
id: 8a839c48636606501d1b09043f24fca75c0889a610078711ff3468929542464b
title: reduce
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-core-functions-reduce.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-core-functions-reduce
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 55dd1eea94f831e2d17e4cd6967e6521b1692908b8e0cafc79899c4ebbfce80c
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# reduce

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## reduce<T>(@StreamCapable items: Array<T>, callback: (item: T, accumulator: T) -> T): T | Null

Applies a reduction expression to the elements in an array.

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 that if the array is empty and no default value is set on the accumulator parameter, a null
value is returned.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>item</code></p></td><td><p>Item in the input array. It provides the value to reduce. Can also be referenced as <code>$</code>.</p></td></tr><tr><td><p><code>acc</code></p></td><td><p>The accumulator. Can also be referenced as <code>$$</code>. Used to store the result of the lambda expression after each iteration of the <code>reduce</code> operation.</p><p>The accumulator parameter can be set to an initial value using the syntax <code>acc = initValue</code>. 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.</p><p>If an initial value for the accumulator is not set, the accumulator is set to the first element of the input array. Then the lambda expression is called with the second element of the input array.</p><p>The initial value of the accumulator and the lambda expression dictate the type of result produced by the <code>reduce</code> function. If the accumulator is set to <code>acc = {}</code>, the result is usually of type <code>Object</code>. If the accumulator is set to <code>acc = []</code>, the result is usually of type <code>Array</code>. If the accumulator is set to <code>acc = ""</code>, the result is usually a <code>String</code>.</p></td></tr></tbody></table>

### Example

This example returns the sum of the numeric values in the first input array.

#### Source

```DataWeave
%dw 2.0
output application/json
---
[2, 3] reduce ($ + $$)
```

#### Output

```JSON
5
```

### Example

This example adds the numbers in the `sum` example, concatenates the same numbers in `concat`, and
shows that an empty array `[]` (defined in `myEmptyList`) returns `null` in `emptyList`.

#### Source

```DataWeave
%dw 2.0
var myNums = [1,2,3,4]
var myEmptyList = []
output application/json
---
{
   "sum" : myNums reduce ($$ + $),
   "concat" : myNums reduce ($$ ++ $),
   "emptyList" : myEmptyList reduce ($$ ++ $)
}
```

#### Output

```JSON
{ "sum": 10, "concat": "1234", "emptyList": null }
```

### Example

This example sets the first element from the first input array to `"z"`, and it adds `3` to the sum
of the second input array. In `multiply`, it shows how to multiply each value in an array by the
next (`[2,3,3] reduce ((item, acc) → acc * item)`) to produce a final result of `18` (=
`2 * 3 * 3`). The final example, `multiplyAcc`, sets the accumulator to `3` to multiply the result
of `acc * item` (= `12`) by `3` (that is, `3 (2 * 2 * 3) = 36`), as shown in the output.

#### Source

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

#### Output

```JSON
{ "concat": "zabcd", "sum": 18, "multiply": 18, "multiplyAcc": 36 }
```

### Example

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

#### Source

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

#### Output

```JSON
"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<T, A>(@StreamCapable items: Array<T>, callback: (item: T, accumulator: A) -> A): A

## reduce(@StreamCapable text: String, callback: (item: String, accumulator: String) -> String): String

Applies a reduction expression to the characters in a string.

For each character of the input string, 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 character and the current accumulator value.

Note that if the string is empty and no default value is set on the accumulator parameter, an empty
string is returned.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The string to reduce.</p></td></tr><tr><td><p><code>callback</code></p></td><td><p>The function to apply.</p></td></tr></tbody></table>

### Example

This example shows how `reduce` can be used to reverse a string.

#### Source

```DataWeave
%dw 2.0
output application/json
---
"hello world" reduce (item, acc = "") -> item ++ acc
```

#### Output

```Json
"dlrow olleh"
```

## reduce<A>(@StreamCapable text: String, callback: (item: String, accumulator: A) -> A): A

## reduce<T, A>(@StreamCapable items: Null, callback: (item: T, accumulator: A) -> A): Null

Helper function that enables `reduce` to work with a `null` value.
