Perform Formatting on All Values in an Array
Perform formatting on all values in a flat array using the array:eval()
function
Query
array:eval("devices[]", asArray="upperDevices[]", var=d, function={upperDevices :=upper("d")})
Introduction
The array:eval()
function is used to apply a
specific function to each value inside an array. The
array:eval()
function processes every item in
the list (array) one by one, performs some kind of calculation or
operation, and either overwrites the original array or saves the
result in a new array.
In this example, the
array:eval()
function is used to convert all
values (for example [Thermostat, Smart
Light]
) in an array
devices[] from lowercase
to uppercase and show the results in a new array.
Example incoming data might look like this:
{\"devices\":[\"Thermostat\",\"Smart Plug\"],\"room\":\"Kitchen\"}" |
{\"devices\":[\"Smart Light\",\"Thermostat\",\"Smart Plug\"],\"room\":\"Living Room\"}" |
{\"devices\":[\"Smart Light\",\"Smart Plug\"],\"room\":\"Bedroom\"}" |
{\"devices\":[\"Thermostat\",\"Smart Camera\"],\"room\":\"Hallway\"}" |
{\"devices\":[\"Smart Light\",\"Smart Lock\"],\"room\":\"Front Door\"}" |
Step-by-Step
Starting with the source repository events.
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0{{Aggregate}} result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;logscale
array:eval("devices[]", asArray="upperDevices[]", var=d, function={upperDevices :=upper("d")})
Formats all values in the array devices[] to uppercase and returns the results in a new array named upperDevices[]. The values in the original array stay the same:
[Thermostat, Smart Plug, Smart Light]
and the new array contains the returned results:[THERMOSTAT, SMART PLUG, SMART LIGHT]
Event Result set.
Summary and Results
The query is used to turn values in an array into uppercase. The
array:eval()
function can also be used for
squaring a list of numbers in an array.
Sample output from the incoming example data:
devices[] | upperDevices[] |
---|---|
Thermostat | THERMOSTAT |
Smart Plug | SMART PLUG |
Smart Light | SMART LIGHT |