KEEP

KEEP returns an array of elements that have been filtered based on a condition.

KEEP is a very flexible function, and is especially so because it can interpret dynamic references. These can be put to great use when building conditions.

Function category: Collection

KEEP has the same functionality as FILTER. However, FILTER is a legacy function and has been superseded by KEEP.

Syntax

KEEP(arg1, arg2(_))

Arguments

Description

arg1

Condition which will be evaluated on members of arg2.

arg2

Array of values.

_

Dynamic reference to the elements of arg2.

Examples

Example 1: Return only specific year ranges

Let's say we're given a response with the following vehicle information:

{
"data":{
"vehicles":[
{
"make":"Toyota",
"year":1999,
"miles":50000
},
{
"make":"BMW",
"year":2002,
"miles":80000
},
{
"make":"Mercedes Benz",
"year":2000,
"miles":20000
}
],
"models":[
"Toyota",
"BMW",
"Mercedes Benz"
],
"years":[
1999,
2000,
2002
]
}
}

Let's say we want to restrict the years shown to those from 2000 and later. To do that, we could use KEEP:

KEEP(FN(x, x >=2000), data.years)

This would return the following:

[2000, 2002]

Example 2: Limit vehicles returns by year

If we wanted to limit the vehicles returned by year of model, we could invoke a dynamic reference (the _ character):

KEEP(FN(x, x.year >= 2000), data.vehicles)

This would return the following:

[{"make":"BMW", "year":2002, "miles":80000}, {"make":"Mercedes Benz", "year":2000, "miles":20000}]

Example 3: Limit by model names by length of name

If we wanted to limit the vehicles returned by length of the name, we could invoke the dynamic reference (the _ character):

KEEP(FN(x, LEN(x) > 3), data.models)

This would return the following:

["Toyota", "Mercedes Benz"]