Reference › Function reference › Functions

Functions.collections.cleanArray

public

Call as

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

Record it as a dependency named Functions.collections.cleanArray (accepting the completion does this), then call it by that name.

What it does

Remove entries in place, preserving only keys listed among except values by default. With invert true, remove listed keys and retain unlisted keys. Without an exception list, the default removes every entry and invert true retains every entry. Nil input does nothing.

Arguments

NameTypeWhat it does
arraytable | nilTable to modify, or nil to do nothing.
excepttable | nilOptional collection of key values selecting entries; its own keys are ignored.
invertboolean | nilTrue removes listed entries; false or nil preserves only listed entries.

Returns

1...No return values. The supplied table is modified in place.

Editions

Built In helpers are registered on every edition. What differs is the game API they read; where one is missing the helper falls back or returns nil with a reason.

It reads no game API directly.

Example

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

Source

Functions/collections.lua, line 109. Read only in game; Duplicate makes an editable copy.

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

Back to Function reference