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

# 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
}
```
