[9,2,3,4,5] filter (value, index) -> (value > 2)
filter
filter<T>(@StreamCapable items: Array<T>, criteria: (item: T, index: Number) -> Boolean): Array<T>
Iterates over an array and applies an expression that returns matching values.
The expression must return true
or false
. If the expression returns true
for a value or index in the array, the value gets captured in the output array.
If it returns false
for a value or index in the array, that item gets
filtered out of the output. If there are no matches, the output array will
be empty.
Parameters
Name | Description |
---|---|
|
The array to filter. |
|
Boolean expression that selects an |
Example
This example returns an array of all the users with age bigger or equal to 30. The script accesses data of each element from within the lambda expression.
Example
This example returns an array of all items found at an index ($$
)
greater than 1
where the value of the element is less than 5
. Notice that
it is using anonymous parameters as selectors instead of using named
parameters in an anonymous function.
Example
This example reads a JSON array that contains objects with user
and error
keys, and uses the filter
function to return only the objects in which the value of the error
key is null
.
Example
This example reads a JSON array and uses the filter
function to extract the phone numbers that are active.
Source
%dw 2.0
output application/json
---
{
id: payload.Id,
markCode: payload.marketCode,
languageCode: payload.languageCode,
username: payload.profile.base.username,
phoneNumber: (payload.profile.base.phone filter ($.activeInd == "Y" and $.primaryInd== "Y")).number default []
}
Input
{
"Id": "1184001100000000517",
"marketCode": "US",
"languageCode": "en-US",
"profile": {
"base": {
"username": "TheMule",
"activeInd": "R",
"phone": [
{
"activeInd": "Y",
"type": "mobile",
"primaryInd": "Y",
"number": "230678123"
},
{
"activeInd": "N",
"type": "mobile",
"primaryInd": "N",
"number": ""
},
{
"activeInd": "Y",
"type": "mobile",
"primaryInd": "Y",
"number": "154896523"
}
]
}
}
}
filter(@StreamCapable text: String, criteria: (character: String, index: Number) -> Boolean): String
Iterates over a string and applies an expression that returns matching values.
The expression must return true
or false
. If the expression returns true
for a character or index in the array, the character gets captured in the output string.
If it returns false
for a character or index in the array, that character gets
filtered out of the output. If there are no matches, the output string will
be empty.