---
schema_version: "1"
id: faff6dd058e4cb0b00fc7cde4455c81c821d39cfbc74a13b1aa588e3fe9f180d
title: sumBy
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dw-arrays-functions-sumby.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dw-arrays-functions-sumby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0de5b8f912b98335ca8878a95f80d132b9e3f3eb67f61dafaccd77a263cade0e
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
---

# sumBy

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

## sumBy<T>(@StreamCapable array: Array<T>, numberSelector: (T) -> Number): Number

Returns the sum of the values of the elements in an array.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>array</code></p></td><td><p>The input array.</p></td></tr><tr><td><p><code>numberSelector</code></p></td><td><p>A DataWeave selector that selects the values of the numbers in the input array.</p></td></tr></tbody></table>

### Example

This example calculates the sum of the values of elements some arrays. Notice that both of the
`sumBy` function calls produce the same result.

#### Source

```DataWeave
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
  "sumBy" : [
    [ { a: 1 }, { a: 2 }, { a: 3 } ] sumBy $.a,
    sumBy([ { a: 1 }, { a: 2 }, { a: 3 } ], (item) -> item.a)
  ]
}
```

#### Output

```json
{ "sumBy": [6, 6] }
```

## sumBy(array: Null, numberSelector: (Nothing) -> Any): Null

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

_Introduced in DataWeave version 2.4.0._
