---
schema_version: "1"
id: 26af4537749b14bb07a7caebeb6a49364bfa7cad33b0cd1e7e398f666f6b7692
title: sumBy
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-arrays-functions-sumby.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-arrays-functions-sumby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 45ccb5fe2b6a36d85e072b883c3994048d8a6b7d0567383d69119a158a5715a5
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# 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._
