Contact Free trial Login

filterObject

filterObject({ (K)?: V }, (value: V, key: K, index: Number) -> Boolean): { (K)?: V }

Filters an object, keeping the key-value pairs that fulfill the criteria.

The criteria (a lambda expression) has three parameters: value, key, and index. You can reference the value with $, the key with $$, and the index with $$$.

Parameters

Name Description

value

The input object.

criteria

The expression that determines whether to retain the key-value pair or not.

Example

This example keeps only the key-value pairs if the value equals "a".

Source

%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b"} filterObject ((value) -> value == "a")

Output

{
  "letter1": "a"
}

Example

This example only keeps the key-value pairs where the key starts with "letter".

Source

%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b", "id": 1} filterObject ((value, key) -> key startsWith "letter")

filterObject(Null, (value: Nothing, key: Nothing, index: Nothing) -> Boolean): Null

Helper function that allows filterObject to work with null values.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub