我如何检查一个表(作为数组)是否包含另一个表的所有键?类似于PHP中的array_diff_key:https://www.php.net/manual/fr/function.array-diff-key.php
此函数应返回,例如,[“ firstLevel”] [“ e”],因为此元素在tblTwo中不存在:
tblOne = {
["firstLevel"] = {
["a"] = "something",
["b"] = "something",
["c"] = "something",
["subLevel"] = {
["d"] = "something",
["e"] = "something",
}
}
tblTwo = {
["firstLevel"] = {
["a"] = "something",
["b"] = "something",
["c"] = "something",
["subLevel"] = {
["d"] = "something",
}
}
尝试下面的代码:
function diff(a,b,s)
for k,v in pairs(a) do
if b[k]==nil then return s.."."..k end
if type(v)=="table" then
local w=diff(v,b[k],k)
if w~=nil then return s.."."..w end
end
end
end
print(diff(tblOne,tblTwo,"TOP"))