Lua - merge tables?

前端 未结 8 1500
忘掉有多难
忘掉有多难 2020-11-28 06:45

I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don\'t seem to

8条回答
  •  萌比男神i
    2020-11-28 07:01

    Wouldn't this work properly?

    
    function merge(t1, t2)
        for k, v in pairs(t2) do
            if (type(v) == "table") and (type(t1[k] or false) == "table") then
                merge(t1[k], t2[k])
            else
                t1[k] = v
            end
        end
        return t1
    end
    

提交回复
热议问题