--table 是否包含某个元素function table.contains(table, element)if table ==nilthenreturnfalseendfor _, value inpairs(table)doif value == element thenreturntrueendendreturnfalseend
table长度
function table.count(table)local count =0for k, v inpairs(table)do
count = count +1endreturn count
end
合并table
-- 合并数组function table.addRange(sourceTable, addTable)for k, v inpairs(addTable)do
table.insert(sourceTable, v)endend-- 合并tablefunction table.merge(sourceTable, addTable)for k, v inpairs(addTable)do
sourceTable[k]= v
endend
复制table
-- 复制tablefunction table.copy(st)local tab ={}for k, v inpairs(st or{})doiftype(v)~="table"then
tab[k]= v
else
tab[k]= table.copy(v)endendreturn tab
end-- 复制table 到 源tablefunction table.copyTo(source, des)local tab = des
for k, v inpairs(source or{})doiftype(v)~="table"then
tab[k]= v
else
tab[k]= table.copy(v)endendend
获取index
function table.indexOf(list, target, from, useMaxN)local len =(useMaxN and#list)or table.maxn(list)if from ==nilthen
from =1endfor i = from, len doif list[i]== target thenreturn i
endendreturn-1end
反转table
--反转tablefunction table.reverse(t)local l =#t
local c = math.floor(l /2)for i =1, c dolocal v = t[i]
t[i]= t[l - i +1]
t[l - i +1]= v
endend
table map
-- function table.map(t, fun)local l ={}for k, v inpairs(t)do
l[k]=fun(v, k)endreturn l
end
过滤table
-- 过滤tablefunction table.select(t, fun)local l ={}for k, v inpairs(t)doiffun(v, k)then
l[k]= v
endendreturn l
end