Dictionary 如何将lua表中的值格式化为:t={[';foo';]=true,[';bar';]=true}?

Dictionary 如何将lua表中的值格式化为:t={[';foo';]=true,[';bar';]=true}?,dictionary,lua,key-value,lua-table,key-pair,Dictionary,Lua,Key Value,Lua Table,Key Pair,这与前面的问题有关: 基本上,函数依赖于以下格式的数据: local vendors = { Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true}, Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons

这与前面的问题有关:

基本上,函数依赖于以下格式的数据:

local vendors = {
                 Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true}, 
                 Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true},
                 Spar ={Nestle = true, Johnsons = true, Pampers = true, Simple = true}
               }
但是,我通过循环路径位置并将它们添加到一个表中来收集数据,该表只创建一个列表,例如:

Asda = {"Kellogg", "Cadbury", "Nestle", "Johnsons", "Pampers", "Simple"}
我还有另一种方法可以添加它们:

local Asda = {}
for index = 1, 9 do 
local pathAsda = factReference -- some path location which changes by index increasing
if pathAsda ~= "" then
    Asda[#Asda+1] = {[Asda] = true} -- table.insert(Asda, pathAsda), for the previously mentioned format
end
这将给我留下:

 Asda= {{Kellogg = true}, {Cadbury = true}, {Nestle = true}, {Johnsons = true}, {Pampers = true}, {Simple = true}}
然后我会使用:

table.insert(vendorSources,Asda)
这两种格式都不适用于答案中的函数,我似乎不知道如何修改任何部分以使其起作用

function intersection(s1, s2) -- finds out if two sets (s1 & s2) overlap
local output = {}

  for key in pairs(s1) do
    output[#output + 1] = s2[key]
  end
return output
end

是否有方法编辑任何一个列表(Asda)以使其格式正确?

您需要使用
Asda[pathAsda]=true
而不是
Asda[#Asda+1]={[pathAsda]=true}
,但请记住,在这种情况下订单元素是不保证的。

谢谢@Paul,你知道当你看一个函数太久的时候…看起来很明显!订单不是此问题的问题