Lua - merge tables?

前端 未结 8 1418
忘掉有多难
忘掉有多难 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条回答
  • 2020-11-28 07:09
    for k,v in pairs(second_table) do first_table[k] = v end
    
    0 讨论(0)
  • 2020-11-28 07:14

    Doug Currie's answer is the simplest for most cases. If you need more robust merging of tables, consider using the merge() method from the Penlight library.

    require 'pl'
    pretty.dump(tablex.merge({a=1,b=2}, {c=3,d=4}, true))
    
    -- {
    --   a = 1,
    --   d = 4,
    --   c = 3,
    --   b = 2
    -- }
    
    0 讨论(0)
提交回复
热议问题