参考 › 函数参考 › Functions

Functions.collections.cleanArray

公共

调用方式

Functions.collections.cleanArray(array, except, invert)

将其记录为名为 Functions.collections.cleanArray 的依赖项(接受自动补全即可完成),然后按该名称调用。

作用

原地移除条目,默认只保留 except 值中列出的键。invert 为 true 时,移除列出的键并保留未列出的键。没有例外列表时,默认会移除所有条目,而 invert 为 true 时会保留所有条目。输入为 nil 时不执行任何操作。

参数

名称类型作用
arraytable | nil要修改的表;为 nil 时不执行任何操作。
excepttable | nil可选的键值集合,用于选择条目;其自身的键会被忽略。
invertboolean | nil为 true 时移除列出的条目;为 false 或 nil 时只保留列出的条目。

返回值

1...没有返回值。所提供的表会被原地修改。

版本

内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。

它不直接读取任何游戏 API。

示例

local seen = { Moonfire = true, Sunfire = true, Rake = true }
-- invert: remove the listed keys, keep the rest
Functions.collections.cleanArray(seen, { "Rake" }, true)
return seen.Rake == nil and seen.Moonfire == true

源码

Functions/collections.lua,第 109 行。在游戏中为只读;“复制”会生成一份可编辑的副本。

Functions.collections.cleanArray(array, except, invert)
    if array == nil then
        return
    end
    if invert == nil then
        invert = false
    end

    for k,_ in pairs(array) do
        local skip = invert
        if except ~= nil then
            for _,v2 in pairs(except) do
                if (k == v2) then
                    skip = not invert
                    break
                end
            end
        end
        if not skip then
            array[k] = nil
        end
    end

返回函数参考