Filter an Array on a Given Condition
Filter the elements of a flat array on a given condition using the array filter function array:filter()
Query
array:filter(array="mailto[]", var="addr", function={addr=ba*@example.com}, asArray="out[]")
Introduction
It is possible to filter an array on a given condition using the
array filter function array:filter()
. The
array:filter()
creates a new array with
elements matching the specified conditions and does not change the
original array. The new array will retain the original order.
Example incoming data might look like this:
mailto[0]=foo@example.com
mailto[1]=bar@example.com
mailto[2]=baz@example.com
Step-by-Step
Starting with the source repository events.
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0[[Array Manipulation]] result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;logscale
array:filter(array="mailto[]", var="addr", function={addr=ba*@example.com}, asArray="out[]")
Filters the mailto[] array to include only elements that contain the value
ba*@example.com
, this is achieved by testing the value of each element of the array, set by thevar
parameter asaddr
, returning a new array that only contains elements that meet the specified condition. The expression in thefunction
argument should contain the field declared in theaddr
parameter. Event Result set.
Summary and Results
The query is used to filter values from the input array using the function provided in the array and return a new array with the results meeting the specified condition.
Sample output from the incoming example data:
out[0]=bar@example.com
out[1]=baz@example.com