123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- --[[
- Descripttion:
- version:
- Author: Neo,Huang
- Date: 2022-07-04 11:24:54
- LastEditors: Neo,Huang
- LastEditTime: 2022-07-05 10:11:23
- --]]
- local root = {}
- -- 合并列表 - 物品ID不重复
- function root:items_merge(dst, src)
- if is_empty(src) then
- return dst
- end
- for k, v in ipairs(src) do
- dst = root.add_item_to_items(dst, v)
- end
- return dst
- end
- -- 合并 - 物品ID不重复
- function root:add_item_to_items(dst, item)
- if is_empty(item) then
- return dst
- end
- local items = table.copy(dst or {}, true)
- local isMatch = false
- for k, v in ipairs(items) do
- if v.id == item.id then
- if v.count == nil then
- v.count = 0
- end
- v.count = v.count + item.count
- isMatch = true
- end
- end
- if not isMatch then
- table.insert(items, item)
- end
- return items
- end
- return root
|