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

# sumBy

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

## sumBy<T>(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._
