Skip to content

arrayContains - Test if array contains an element

The arrayContains function returns true if an array includes a matching element, or false if that element is not included. arrayContains is the mirror version of inArray.

Syntax

Like many functions in DataPrime, arrayContains supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

arrayContains(array: array<T>, element: T): bool
array: array<T>.arrayContains(element: T): bool

Arguments

NameTypeRequiredDescription
arrayarray of type TtrueT must be either string, bool, number, interval, timestamp, regexp or enum
elementTtrueT must be either string, bool, number, interval, timestamp, regexp or enum

Example - Check if a particular IP appears in a block list

Consider the following log, from a firewall:

{
    "action": "BLOCK",
    "client_ip": "134.56.32.98",
    "blocked_ips": [
        "134.56.32.91",
        "134.56.32.93",
        "134.56.32.90",
        "134.56.32.105"
    ]
}

We can see that the firewall blocked a given IP address. If we want to check our client_ip against the block list, we can do this using arrayContains:

create is_blocked_ip from arrayContains(blocked_ips, client_ip)
create is_blocked_ip from blocked_ips.arrayContains(client_ip)

This will create a field, is_blocked_ip that is true if the client_ip field appears in the blocked_ips list, otherwise false.