Reference › Function reference › Functions

Functions.collections.index

public

Call as

Functions.collections.index(array, value)

Record it as a dependency named Functions.collections.index (accepting the completion does this), then call it by that name.

What it does

Return the first key whose value equals the supplied value during pairs traversal. Nil or false input tables and absent values return nil. Multiple matching keys have no guaranteed selection order.

Arguments

NameTypeWhat it does
arraytable | nil | falseTable to search; nil or false returns nil.
valueanyValue compared with each table value using Lua equality.

Returns

1anyMatching key, including a false key, or nil when no match exists.

Editions

Built In helpers are registered on every edition. What differs is the game API they read; where one is missing the helper falls back or returns nil with a reason.

It reads no game API directly.

Example

local priority = { "Fire", "Frost", "Arcane" }
local slot = Functions.collections.index(priority, "Frost")
return slot ~= nil and slot <= 2

Source

Functions/collections.lua, line 76. Read only in game; Duplicate makes an editable copy.

Functions.collections.index(array, value)
    if not array then return nil end

    for idx,v in pairs(array) do
        if (v == value) then
            return idx
        end
    end

Back to Function reference