table_ext.lua
---如果table不为空则新建 function table.getEmptyTable(tb) if nil == tb or nil ~= next(tb) then return {} end return tb end function table.isEmpty(tb) return nil == tb or nil == next(tb) end function table.swap(tb, k1, k2) local temp = tb[k1] tb[k1] = tb[k2] tb[k2] = temp end function table.mergeTable(tb, mergeTb) local count = 0 for k, v in pairs(mergeTb) do tb[k] = v count = count + 1 end return tb, count end
查找
function table.findItem(tb, item) for k, v in pairs(tb) do if v == item then return k, v end end return nil end function table.findMatch(tb, matchFunc) for k, v in pairs(tb) do if matchFunc(k, v) then return k, v end end return nil end function table.ipairsFindItem(tb, item) for i, v in ipairs(tb) do if v == item then return i, v end end return -1 end function table.ipairsFindMatch(tb, matchFunc) for i, v in ipairs(tb) do if match(i, v) then return i, v end end return -1 end
标签:function,end,nil,常用工具,用法,return,table,tb From: https://www.cnblogs.com/sailJs/p/17407611.html