---
schema_version: "1"
id: 60e02c3e233172543354b067dacbe477ceaa18718042f769d56af964a1331e7b
title: firstWith
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-arrays-functions-firstwith.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-arrays-functions-firstwith
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d93e685e94eff13824959150f6d744e4db8decb608cdc8a4b79e2262091e4999
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# firstWith

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

## firstWith<T>(array: Array<T>, condition: (item: T, index: Number) -> Boolean): T | Null

Returns the first element that satisfies the condition, or returns `null` if no element meets the
condition.

_Introduced in DataWeave version 2.3.0._

### 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 array of elements to search.</p></td></tr><tr><td><p><code>condition</code></p></td><td><p>The condition to satisfy.</p></td></tr></tbody></table>

### Example

This example shows how `firstWith` behaves when an element matches and when an element does not
match.

#### Source

```DataWeave
%dw 2.0
output application/json
import firstWith from dw::core::Arrays
var users = [{name: "Mariano", lastName: "Achaval"}, {name: "Ana", lastName: "Felisatti"}, {name: "Mariano", lastName: "de Sousa"}]
---
{
  a: users firstWith ((user, index) -> user.name == "Mariano"),
  b: users firstWith ((user, index) -> user.name == "Peter")
}
```

#### Output

```Json
{
  "a": {
    "name": "Mariano",
    "lastName": "Achaval"
  },
  "b": null
}
```

## firstWith(array: Null, condition: (item: Nothing, index: Nothing) -> Any): Null

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

_Introduced in DataWeave version 2.4.0._
