(LUA) 是否有从 table 获取密钥的函数或方法

(LUA) Is there a function or method to get the KEY from a table

    {
    ["Question"] = "A stupid one";
    }

获取 table 的第一个密钥的最佳方法是什么?

我想我可以遍历循环,但肯定有更好的方法或内置函数。

谢谢!

(这是我第一次使用 Whosebug,所以如果我做错了什么,我深表歉意)

您仍然需要迭代才能获得密钥。

您可以对任何键使用 pairs,对从 1 开始的连续整数键使用 ipairs

pairs 使用 next 函数 returns table 中的下一个键,但它不能用于在不迭代的情况下查找特定键。

要查找值“A stupid one”的键,请使用:

for k,v in pairs(t) do
    if v == "A stupid one" then
       print("Key is:", k)
       break
    end
end