Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
Daniel Gorman edited this page May 21, 2019 · 7 revisions

The FILTER Function

Function Group: Collection

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

Syntax

FILTER(arg1, arg2(_))

  • arg1 is the condition which will be evaluated on members of arg2
  • arg2 is an array of values
  • _ is the dynamic reference to the elements of arg2

Uses

Let's say we're given a response with some vehicle information that looks like this:

{  
   "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 our shown years to those from 2000 and above. To do that with the above data, we could use a function like this:

FILTER(_ >= 2000, data.years)

This would return [2000, 2002].

Other examples:

FILTER 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.

For instance, if we wanted to limit the vehicles returned, we could invoke a dynamic reference (the _ character) in the context of a filter, like this:

FILTER(_.year >= 2000, data.vehicles) => [{"make":"BMW", "year":2002, "miles":80000}, {"make":"Mercedes Benz", "year":2000, "miles":20000}]

FILTER(LEN(_) > 3, data.models) => ["Toyota", "Mercedes Benz"]

Notes

Functions can be opinionated about their arguments - the LEN function, for instance, can only be called on strings. COUNT can only be called on numbers; this is something to be aware of when using FILTER or other functions that can accept conditions or functions as arguments.

Clone this wiki locally