TableUtil.lua 1.21 KB
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