inArray
Description
Returns true if the specified element exists within the array, or false if it does not.
- This function is the inverse of
arrayContains. - Supported element types include
string,bool,number,interval,timestamp,regexp, andenum.
Syntax
Like many functions in DataPrime, inArray supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
inArray(element: T, array: array<T>): bool
(element: T).inArray(array: array<T>): bool
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
element | T | true | The element to check for in the array |
array | array<T> | true | The array to search, must contain elements of the same type as element |
Example
Use case: Check if a client IP appears in a block list
Suppose you have a log entry with a client IP. Consider the following input:
{
"client_ip": "192.168.1.105"
}
By checking whether the client_ip value exists in an array of blocked IP addresses, you can identify whether the request should be blocked.
Example query
- Function notation
- Method notation
filter inArray(client_ip, ['192.168.1.105', '192.168.1.112', '192.168.1.32'])
filter client_ip.inArray(['192.168.1.105', '192.168.1.112', '192.168.1.32'])
Example output
The result will return true since "192.168.1.105" is included in the array:
{
"client_ip": "192.168.1.105",
"is_blocked": true
}