---
schema_version: "1"
id: 1e7621e03684dfc06dc3dac594bff8ec99365aa850745162cbf374c16e1c8a9d
title: takeWhile
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dw-objects-functions-takewhile.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dw-objects-functions-takewhile
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 71e5a2ff51bceb2d2faac1f707349f7194bd76e55c12d101c8e77ad88a772fef
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
---

# takeWhile

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

## takeWhile<T>(obj: Object, condition: (value: Any, key: Key) -> Boolean): Object

Selects key-value pairs from the object while the condition is met.

_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>obj</code></p></td><td><p>The object to filter.</p></td></tr><tr><td><p><code>condition</code></p></td><td><p>The condition (or expression) used to match a key-value pairs in the object.</p></td></tr></tbody></table>

### Example

This example iterates over the key-value pairs in the object and selects the elements while the
condition is met. It outputs the result into an object.

#### Source

```DataWeave
%dw 2.0
import * from dw::core::Objects
output application/json
var obj = {
  "a": 1,
  "b": 2,
  "c": 5,
  "d": 1
}
---
obj takeWhile ((value, key) ->  value < 3)
```

#### Output

```json
{
  "a": 1,
  "b": 2
}
```
