KEEP

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

KEEP is a very flexible function especially because it can interpret dynamic references. These can be very useful 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
]
}
}

If we want to restrict the years shown to those from 2000 and later, use the following function.

# Filter for years 2000 and later
KEEP(FN(x, x >=2000), data.years)

This returns 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) using the following function.

# Filter vehicles by year of model
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: Filter using the dynamic reference character

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

# Filter vehicles by length of name using a dynamic character reference
KEEP(FN(x, LEN(x) > 3), data.models)

This would return the following.

[
"Toyota",
"Mercedes Benz"
]