index() Function

Returns the data[index] if it is valid or else returns the default value.

Syntax

index( data, index, [default] )

data: (Any Type) A homogeneous array, whose indexed value is expected. Can also be a dictionary, map, CDT, or Record.

index: (Any Type) The index or array of indices of the data. Index can be an integer, text, or a record type field reference.

default: (Any Type) The default value to be returned if the data or the index is invalid, e.g. if the data itself is empty or the index is not found in the data. The type of the default value must be the same as that of the elements of the data. This parameter is optional.

Returns

Any Type

Notes

The type of the default value must be same as that of the elements in the array.

Examples

You can experiment with this function in the test box below.

Test Input

Retrieving a value at a single index:

index({10, 20, 30}, 2, 1) returns 20

Return the value of a field name within a CDT where the array is the PV and the index is the field name:

index(pv!person, "firstName", "") returns the value of the "firstName" field in a CDT of person with two fields (firstName and lastName)

Retrieving values at multiple indices:

index({10, 20, 30}, {1, 3}, 0) returns 10, 30

Retrieving values at repeated indices:

index({10, 20, 30}, {1, 1, 3}, 0) returns 10, 10, 30

Retrieving the value of a CDT field using the field name as the index argument:

index(topaginginfo(1, 10), "startIndex", 0) returns 1

Retrieving the value of a map field using the field name as the index argument:

index(a!map(a: 1, b: 2), "b", 0) returns 2

Retrieving the value of a record using the record field reference as the index argument:

index(recordType!Department(name: "Engineering"), recordType!Department.fields.name, "") returns "Engineering"

See Also

property(): This function acts as an alias to the index() function especially when applied over custom data types.

Open in Github

On This Page

FEEDBACK