---
schema_version: "1"
id: 2f8bca74dde6ec69714775dffadae87e9d176a33e40f006a6eccfb61a6383ede
title: takeWhile
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-arrays-functions-takewhile.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-arrays-functions-takewhile
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3725a631a94054a87e6f2473566df8582d122aba40d8dc6dc993ee03aa86874d
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# takeWhile

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

## takeWhile<T>(array: Array<T>, condition: (item: T) -> Boolean): Array<T>

Selects elements from the array while the condition is met but stops the selection process when it
reaches an element that fails to satisfy the condition.

To select all elements that meet the condition, use the `filter` function.

_Introduced in DataWeave version 2.2.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.</p></td></tr><tr><td><p><code>condition</code></p></td><td><p>The condition (or expression) used to match an element in the array.</p></td></tr></tbody></table>

### Example

This example iterates over the elements in the array and selects only those with an index that is
`<= 1` and stops selecting elements when it reaches one that is greater than `2`. Notice that it
does not select the second `1` because of the `2` that precedes it in the array. The function
outputs the result into an array.

#### Source

```DataWeave
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,1]
---
arr takeWhile $ <= 1
```

#### Output

```json
[0, 1]
```

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

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

_Introduced in DataWeave version 2.4.0._
