Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
{ ["Question"] = "A stupid one"; }
获取表的第一个键的最佳方法是什么?
我在想我可以遍历循环,但肯定有更好的方法或内置函数。
谢谢!
(这是我第一次使用 StackOverflow,如果我做错了什么,我深表歉意)
您仍然需要迭代才能获得密钥。
您可以pairs用于任何键和ipairs从 1 开始的连续整数键。
pairs
ipairs
pairs使用next返回表中下一个键的函数,但如果不迭代它就不能用于查找特定键。
next
要找到值“A愚蠢的”的键,请使用以下命令:
for k,v in pairs(t) do if v == "A stupid one" then print("Key is:", k) break end end