lua - 之后添加值以键入表 lua
问题描述
有人知道如何为已经有值的键添加值吗?
例如:
x = {}
x[1] = {string = "hallo"}
x[1] = {number = 10}
print(x[1].string) --nil
print(x[1].number) --10
应该可以将这两件事都打印出来。同样的方式在这里是可能的:
x[1] = { string = "hallo" ; number = 10}
之后我只需要在表格中添加一些信息,尤其是在同一个键中。谢谢!
解决方案
x = {} -- create an empty table
x[1] = {string = "hallo"} -- assign a table with 1 element to x[1]
x[1] = {number = 10} -- assign another table to x[1]
第二个赋值覆盖第一个赋值。
x[1]["number"] = 10
或 short将向表中x[1].number = 10
添加一个number
具有值的字段10
x[1]
请注意,您x[1] = { string = "hallo" ; number = 10}
实际上等同于
x[1] = {}
x[1]["string"] = "hallo"
x[1]["number"] = 10
推荐阅读
- javascript - 对象的 == 和 === 是否相同?
- r - r knit to pdf/html: pandoc 文档转换失败,错误 2
- visual-studio - IISExpress 远程访问
- tensorflow - Keras,训练期间验证集上的 auc 与 sklearn auc 不匹配
- css - sass css输出中的差距
- c# - ASP.NET MVC 将 DateTime 转换为 UTC 以发送到 API
- django - Django Auth:“此字段是必需的”
- html - 在 Ruby 中使用 Nokogiri 抓取特定标题
- google-sheets - “收到无效的 JSON 有效负载。” 创建新工作表时
- swift - FloatingPoint 扩展 - 与其他 FloatingPoint 类型一起初始化