TableUtil.lua
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
cc.exports.TableUtil = {}
-- 检查table里是否存在
function TableUtil.IsInTable(tbl,value)
for k,v in ipairs(tbl) do
if v == value then
return true;
end
end
return false;
end
-- 遍历数组
function TableUtil.getn(tb)
local cnt = 0;
for k,v in pairs(tb) do
cnt = cnt + 1;
end
return cnt;
end
function TableUtil.indexOf(t,v)
local index = -1;
for i,_v in ipairs(t) do
if _v == v then
index = i;
end
end
return index;
end
function TableUtil.copyTab(st)
if not st then return nil end
local tab = {}
for k, v in pairs(st or {}) do
if type(v) ~= "table" then
tab[k] = v
else
tab[k] = TableUtil.copyTab(v)
end
end
return tab
end
function TableUtil.isEmpty(t)
return _G.next( t ) == nil
end
--数组合并
function TableUtil.merge(t1,t2)
local newT = {};
for i,v in ipairs(t1) do
newT[#newT+1] = v;
end
for i,v in ipairs(t2) do
newT[#newT+1] = v;
end
return newT;
end
--数组合并
function TableUtil.join(t1,t2)
for i,v in ipairs(t2) do
t1[#t1+1] = v;
end
return t1;
end
return TableUtil