Skip to content

Table Shared

The Table module extends Lua's native table capabilities by adding powerful utility functions for enhanced table manipulation.

Table Functions

table.clearForNui()

Returns a copy of the table with all function values removed.

Syntax

lua
table.clearForNui(t)

Parameters

t : table

The table to clean

Return Value

Type : table

The table without function values

Example

lua
local tbl = {
  a = 4,
  b = function()
    print('go')
  end,
  c = 10
}
local tbl2 = table.clearForNui(tbl)
print(json.encode(tbl2))
-- Expected output: tbl2 = {a = 4, c = 10}

table.copy()

Deep copies a table. Unlike "=", it doesn't keep the link between both tables.

Syntax

lua
table.copy(orig)

Parameters

orig : table

The table you want to copy

Return Value

Type : table

The copy of the table

Example

lua
local tbl = {
  value = 5,
  children = {
    value = 6
  }
}
local tbl2 = table.copy(tbl)

table.count()

Counts the number of values inside a table.

Syntax

lua
table.count(_table)

Parameters

_table : table

The table to count elements in

Return Value

Type : integer

The number of values inside the table

Example

lua
local tbl = { a = 3, b = 4 }
local count = table.count(tbl)
-- Expected output : 2

table.extract()

Extracts a value from a table by key and removes that key from the table.

Syntax

lua
table.extract(t, key)

Parameters

t : table

The table to extract from

key : any

The key to extract

Return Value

Type : any

The extracted value

Example

lua
local tbl = {
  name = "John",
  age = 30,
  city = "New York"
}
local name = table.extract(tbl, "name")
print(name)
-- Expected output: "John"
print(json.encode(tbl))
-- Expected output: {age = 30, city = "New York"}

table.filter()

Filters a table based on a callback function.

Syntax

lua
table.filter(t, filterIter, keepKeyAssociation)

Parameters

t : table

The table to filter

filterIter : function

A function to execute for each element in the table. Should return true to keep the element. Called with (element, key, originalTable)

keepKeyAssociation : boolean Optional

Keep the original table keys instead of creating a sequential table
default:false

Return Value

Type : table

The filtered table

Example

lua
local tbl = {
  a = 1,
  b = 2,
  c = 3
}
local filter = function(element, key, tble)
  if element < 2 then
    return false --remove tbl.a
  end
  if key == "c" then
    return false --remove tbl.c
  end
  return true
end
local tbl2 = table.filter(tbl, filter)
print(json.encode(tbl2))
-- Expected output : tbl = {b=2}

table.find()

Returns the first element in the table that satisfies the provided function.

Syntax

lua
table.find(t, func)

Parameters

t : table

The table to search in

func : function

A function to test each element. Should return true when found. Called with (element, key, originalTable)

Return Value

Type : any,any

The found value or false if not found , The key of the found value

Example

lua
local tbl = [5, 12, 8, 130, 44];
local cb = function(element)
  return element > 10
end
local found = table.find(tbl,cb)
print(found)
-- Expected output : 12

table.isEgal()

Compares two tables for equality.

Syntax

lua
table.isEgal(table1, table2, strict, canMissInTable1, canMissInTable2)

Parameters

table1 : table

First table to compare

table2 : table

Second table to compare

strict : boolean Optional

If all keys should be in both tables
default:true

canMissInTable1 : boolean Optional

If table2 keys can miss in table1
default:false

canMissInTable2 : boolean Optional

If table1 keys can miss in table2
default:false

Return Value

Type : boolean

Returns true if tables are equal according to specified parameters

Example

lua
local table1 = { a = 1, b = 2, c = { d = 3 } }
local table2 = { a = 1, b = 2, c = { d = 3 } }
local table3 = { a = 1, b = 2 }

print(table.isEgal(table1, table2))
-- Expected output: true (tables are identical)

print(table.isEgal(table1, table3))
-- Expected output: false (strict comparison by default, table3 is missing key 'c')

print(table.isEgal(table1, table3, true, false, true))
-- Expected output: false (strict but allowing table1 keys to be missing in table3)

print(table.isEgal(table3, table1, true, true, false))
-- Expected output: true (strict but allowing table3 keys to be missing in table1)

table.isEmpty()

Checks if a table is empty.

Syntax

lua
table.isEmpty(_table)

Parameters

_table : table

The table to check

Return Value

Type : boolean

Returns true if the table is empty

Example

lua
local tbl = { a = 10 }
print(table.isEmpty(tbl))
-- Expected output : false

table.map()

Creates a new table populated with the results of calling a function on every element.

Syntax

lua
table.map(t, func)

Parameters

t : table

The table to map

func : function

A function to transform each element. Called with (element, key, originalTable)

Return Value

Type : table

The new mapped table

Example

lua
local tbl = { 1, 4, 9, 16 }
local cb = function(element)
  return element * 2
end
local tbl2 = table.map(tbl, cb)
print(json.encode(tbl2))
-- Expected output : tbl2 = {2,8,18,32}

table.merge()

Merges two tables together.

Syntax

lua
table.merge(t1, t2)

Parameters

t1 : table

The main table

t2 : table

The table to merge

Return Value

Type : table

The merged table. If the same key exists in both tables, only the value of t2 is kept

Example

lua
local tbl1 = {
  a = 5,
  b = 2
}
local tbl2 = {
  a = 10,
  c = 3
}
local tbl3 = table.merge(tbl1, tbl2)
-- Expected output: tbl3 = { a=10, b=2, c=3 }

table.mergeAfter()

Merges the values of the second table sequentially into the first table.

Syntax

lua
table.mergeAfter(t1, t2)

Parameters

t1 : table

The target table to merge into

t2 : table

The table whose values will be appended

Return Value

Type : table

The merged table with values from t2 added at the end of t1

Example

lua
local array1 = { 1, 2, 3 }
local array2 = { 4, 5, 6 }
local result = table.mergeAfter(array1, array2)
print(json.encode(result))
-- Expected output: [1, 2, 3, 4, 5, 6]

-- Unlike table.merge which merges by keys
local tbl1 = { a = 1, b = 2 }
local tbl2 = { c = 3, d = 4 }
local mergeAfterResult = table.mergeAfter(tbl1, tbl2)
print(json.encode(mergeAfterResult))
-- Expected output: {a = 1, b = 2, 1 = 3, 2 = 4}

Last updated: