Contact Us 1-800-596-4880

takeWhile

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

Name Description

obj

The object to filter.

condition

The condition (or expression) used to match a key-value pairs in the object.

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

%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

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