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.


Name Description


The array of elements to search.


The condition to satisfy.


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


%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")


  "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.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!