呼叫方式
Functions.collections.index(array, value)
將它記錄為名為 Functions.collections.index 的相依項目(接受自動完成即可完成此步驟),然後以該名稱呼叫。
作用
在 pairs 走訪過程中傳回第一個值等於所提供值的鍵。輸入表格為 nil 或 false 以及值不存在時傳回 nil。有多個相符的鍵時,選擇順序不確定。
參數
| 名稱 | 類型 | 作用 |
|---|---|---|
array | table | nil | false | 要搜尋的表格;為 nil 或 false 時傳回 nil。 |
value | any | 使用 Lua 相等比較與表格中每個值進行比較的值。 |
傳回值
| 1 | any | 相符的鍵(包括 false 鍵);不存在相符時為 nil。 |
|---|
版本
內建輔助函式在每個版本上都會註冊。不同的是它們讀取的遊戲 API;某個 API 不存在時,輔助函式會改用備用方式,或傳回 nil 並附上原因。
它不會直接讀取任何遊戲 API。
範例
local priority = { "Fire", "Frost", "Arcane" }
local slot = Functions.collections.index(priority, "Frost")
return slot ~= nil and slot <= 2原始碼
Functions/collections.lua,第 76 行。在遊戲中為唯讀;「複製」會產生一份可編輯的複本。
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