feat: add util to remove table content with a func
This commit is contained in:
parent
e211c779a2
commit
1d2de11bc1
1 changed files with 21 additions and 0 deletions
|
@ -152,4 +152,25 @@ function Table.parse(table, structure, nullableNbr)
|
||||||
return parsedTable
|
return parsedTable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Table.remove(table, funcDelete)
|
||||||
|
-- Code from https://stackoverflow.com/questions/12394841/safely-remove-items-from-an-array-table-while-iterating
|
||||||
|
local j, n = 1, #table;
|
||||||
|
|
||||||
|
for i=1,n do
|
||||||
|
if (not funcDelete(table[i], i, j)) then
|
||||||
|
-- Move i's kept value to j's position, if it's not already there.
|
||||||
|
if (i ~= j) then
|
||||||
|
table[j] = table[i];
|
||||||
|
table[i] = nil;
|
||||||
|
end
|
||||||
|
-- Increment position of where we'll place the next kept value.
|
||||||
|
j = j + 1;
|
||||||
|
else
|
||||||
|
table[i] = nil;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return table;
|
||||||
|
end
|
||||||
|
|
||||||
return Table
|
return Table
|
||||||
|
|
Loading…
Reference in a new issue