feat: add util to remove table content with a func

This commit is contained in:
Kazhnuz 2021-08-21 09:42:02 +02:00
parent e211c779a2
commit 1d2de11bc1

View file

@ -152,4 +152,25 @@ function Table.parse(table, structure, nullableNbr)
return parsedTable
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