%dw 2.0
output application/json
---
{"a" : "apple", "b" : "banana"} filterObject ((value) -> value == "apple")
filterObject
filterObject<K, V>(@StreamCapable value: { (K)?: V }, criteria: (value: V, key: K, index: Number) -> Boolean): { (K)?: V }
Iterates a list of key-value pairs in an object and applies an expression that returns only matching objects, filtering out the rest from the output.
The expression must return true
or false
. If the expression returns true
for a key, value, or index of an object, the object gets captured in the
output. If it returns false
for any of them, the object gets filtered out
of the output. If there are no matches, the output array will be empty.
Parameters
Name | Description |
---|---|
|
The source object to evaluate. |
|
Boolean expression that selects a |
Example
This example outputs an object if its value equals "apple"
.
Example
This example only outputs an object if the key starts with "letter". The
DataWeave startsWith
function returns true
or false
. Note that you can
use the anonymous parameter for the key to write the expression
((value, key) → key startsWith "letter")
: ($$ startsWith "letter")`
Example
This example only outputs an object if the index of the object in the array
is less than 1, which is always true of the first object. Note that you can
use the anonymous parameter for the index to write the expression
((value, key, index) → index < 1)
: ($$$ < 1)