Skip to content

table extensions

Side: Shared (client + server)

Extensions to Lua's built-in table library. All functions are added directly to the global table and are available as soon as twinded_libs is loaded.

Functions

table.copy(t)

Deep copy a table with metatable preservation.

Returns: table


table.merge(deepMerge?, ...)

Merge multiple tables into the first one. Keys from later tables overwrite earlier ones.

ParameterTypeDefaultDescription
deepMergeboolean?trueIf true, nested tables are merged recursively
...tableTables to merge (first table is the target)

Returns: table (the first table, modified in place)


table.mergeAfter(...)

Sequential array merge. Appends values from all tables into the first one.

Returns: table


table.isEmpty(t)

Check if a table is nil, empty, or has type "empty".

Returns: boolean


table.count(t)

Count all elements in a table. Works on mixed tables (not just arrays).

Returns: integer


table.filter(t, filterIter, keepKeyAssociation?)

Filter table elements with a predicate function.

ParameterTypeDescription
ttableSource table
filterIterfunctionfunction(value, key, table) -> boolean
keepKeyAssociationboolean?If true, preserves original keys

Returns: table


table.map(t, func)

Apply a function to each element and return a new table.

ParameterTypeDescription
ttableSource table
funcfunctionfunction(value, index, table) -> newValue

Returns: table


table.find(t, func)

Find the first matching element. func can be a function or a direct value for equality check.

ParameterTypeDescription
ttableSource table
funcfunction or anyPredicate or value to match

Returns: value, key — the matching value and its key, or nil if not found.


table.includes(t, value, fromIndex?)

Search for a value in a table.

ParameterTypeDescription
ttableSource table
valueanyValue to search for
fromIndexinteger?Start searching from this index

Returns: boolean, key


table.slice(t, s?, e?)

Extract a sub-table by indices.

ParameterTypeDefaultDescription
ttableSource array
sinteger?1Start index
einteger?#tEnd index

Returns: table


table.clearForNui(t)

Deep copy a table without functions. Safe for JSON serialization and NUI messages.

Returns: table


table.isEgal(t1, t2, strict?, canMissIn1?, canMissIn2?)

Deep comparison of two tables.

ParameterTypeDefaultDescription
t1tableFirst table
t2tableSecond table
strictboolean?trueStrict type comparison
canMissIn1boolean?falseAllow keys present in t2 but missing in t1
canMissIn2boolean?falseAllow keys present in t1 but missing in t2

Returns: boolean


table.extract(t, key)

Extract a value by key and remove it from the table.

ParameterTypeDescription
ttableSource table
keyanyKey to extract

Returns: any — the extracted value.


table.upsert(...)

Set or update a value at a nested path. Creates intermediate tables as needed.

lua
table.upsert(t, {key1, key2, key3}, value)

Returns: table (the root table)


table.doesKeyExist(t, ...)

Check if a nested key path exists.

lua
local exists, value = table.doesKeyExist(t, "settings", "display", "theme")

Returns: boolean, value


table.getDeep(t, keys)

Read a value at a nested key path.

ParameterTypeDescription
ttableSource table
keystableArray of keys forming the path

Returns: value, exists, missingKey


table.deleteDeepValue(t, keys)

Delete a value at a nested key path.

ParameterTypeDescription
ttableSource table
keystableArray of keys forming the path

Returns: table, boolean — the root table and whether deletion succeeded.


table.deleteAndClear(t, keys)

Delete a value at a nested path AND remove empty parent tables left behind.

ParameterTypeDescription
ttableSource table
keystableArray of keys forming the path

Returns: boolean


table.addMultiLevels(...)

Create nested table levels if they don't already exist.

Returns: table (the deepest level)

Examples

Deep copy

lua
local copy = table.copy(original)

Merge tables (config with defaults)

lua
local defaults = { theme = "dark", lang = "en", debug = false }
local userConfig = { lang = "fr" }

local merged = table.merge(defaults, userConfig)
-- { theme = "dark", lang = "fr", debug = false }

Filter and map

lua
local players = {
    { name = "John", age = 25 },
    { name = "Jane", age = 16 },
    { name = "Bob", age = 30 },
}

-- Filter adults
local adults = table.filter(players, function(p) return p.age >= 18 end)

-- Get names only
local names = table.map(adults, function(p) return p.name end)

Find an element

lua
local admin, key = table.find(users, function(u) return u.role == "admin" end)

-- Or find by direct value
local found, key = table.find(names, "John")

Nested operations

lua
local config = {}

-- Set a deeply nested value (creates intermediate tables)
table.upsert(config, {"settings", "display", "theme"}, "dark")

-- Check if the path exists
local exists, value = table.doesKeyExist(config, "settings", "display", "theme")
-- exists = true, value = "dark"

-- Read a nested value
local theme, found, missing = table.getDeep(config, {"settings", "display", "theme"})

-- Delete a nested value and clean up empty parents
table.deleteAndClear(config, {"settings", "display", "theme"})

Safe NUI data

lua
-- Remove functions before sending to NUI
local safeData = table.clearForNui(complexTable)
SendNUIMessage(safeData)

Premium RedM Scripts — Multi-Framework