--------------------------------------------
-----更新Table
-- toTable:旧数据
-- fromTable:新增数据
-- isDeleteOld:是否删除不被匹配的旧数据
--------------------------------------------
function Tool.UpdateToTable(toTable, fromTable, isDeleteOld)
if not toTable then
return fromTable;
end
local kList = {};
for k, v in pairs(toTable) do
kList[k] = true;
end
for k, v in pairs(fromTable) do
if type(v) == "table" then
if kList[k] then
-- 刷新子表
kList[k] = nil;
Tool.UpdateToTable(toTable[k], v);
else
-- 增加字表
toTable[k] = {};
Tool.UpdateToTable(toTable[k], v);
end
else
local oldValue = kList[k];
if oldValue then
-- 刷新数据
kList[k] = nil;
if oldValue ~= v then
toTable[k] = v;
end
else
-- 增加数据
toTable[k] = v;
end
end
end
if isDeleteOld then
for k, _ in pairs(kList) do
-- 删除数据
toTable[k] = nil;
end
end
return toTable;
end
Lua 增量/全量更新table
于 2022-04-30 14:58:45 首次发布