Lua中批量删除数组中的元素

提示:lua中for循环的控制变量是不可更改的;以下代码中的 i = i - 1 不起作用。

for i = 1, #your_table do
	if your_table[i] == removed_value then
		table.remove(your_table, i)
		i = i - 1 -- 不起作用
	end
end

一种可行的办法是从后往前删

删除data_table中所有的2

local data_table = {1, 2, 2, 3, 5, 2, 2, 6, 8, 9, 10}
for i = #data_table, 1, -1 do
	if data_table[i] == 2 then
		table.remove(data_table, i)
	end
end

猜你喜欢

转载自blog.csdn.net/sky_terra/article/details/88357036
今日推荐