Nav

filter

filter(Array<T>, (item: T, index: Number) → Boolean): Array<T>

Returns an array that containing elements that meet the criteria specified by a function (a lambda). The function is invoked with two parameters: value and index.

If the parameters are not named, the index is defined by default as $$, and the value is $. In the next example, the value ($) in the returned array must be greater than 2.

Transform

        
     
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  biggerThanTwo: [1, 2, 3, 4, 5] filter($ > 2)
}
Output

        
     
1
2
3
{
  'biggerThanTwo': [3,4,5]
}

In the next example, the index ($$) of the returned array must be greater than 2.

Transform

        
     
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  indexBiggerThanTwo: [1, 2, 3, 4, 5] filter($$ > 2)
}
Output

        
     
1
2
3
{
  'indexBiggerThanTwo': [4,5]
}

The next example passes named key and value parameters.

Transform

        
     
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
 example3: [0, 1, 2, 3, 4, 5] filter ((key1, value1) -> key1 > 3 and value1 < 5 )
}
Output

        
     
1
2
3
{
  'example3': [4]
}
filter(Null, (item: Nothing, index: Nothing) → Boolean): Null

Helper function that allows filter to work with null values.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.

+