From the reference libraryRedM

autodoc_shared_functions

jo_libs/modules/table/autodoc/autodoc_shared_functions.md

autodoc_shared_functions

Table Functions > table.addMultiLevels()

A function to add a multiples table levels inside a variable if it not exists <br>

Table Functions > table.addMultiLevels() > Syntax

table.addMultiLevels(...)

Table Functions > table.addMultiLevels() > Parameters

... : table|string

if the 1st argument is a table, keys will be injected in it. Else, a new table will be created

Table Functions > table.addMultiLevels() > Return Value

Type : table

The new table with multiples table levels

Table Functions > table.addMultiLevels() > Example

-- Injected inside a value
local value = {x=1}
table.addMultiLevels(value,"key","otherKey")
log(value)
-- Expected output: {"x":1,"key":{"otherKey": {}}}

-- Create a new value
local value = table.addMultiLevels("key","otherKey")
log(value)
-- Expected output: {"key":{"otherKey": {}}

Table Functions > table.clearForNui()

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

Table Functions > table.clearForNui() > Syntax

table.clearForNui(t)

Table Functions > table.clearForNui() > Parameters

t : table

The table to clean

Table Functions > table.clearForNui() > Return Value

Type : table

The table without function values

Table Functions > table.clearForNui() > Example

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


Table Functions > table.copy()

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

Table Functions > table.copy() > Syntax

table.copy(t)

Table Functions > table.copy() > Parameters

t : table

The table you want to copy

Table Functions > table.copy() > Return Value

Type : table

The copy of the table

Table Functions > table.copy() > Example

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


Table Functions > table.count()

Counts the number of values inside a table. <br>

Table Functions > table.count() > Syntax

table.count(t)

Table Functions > table.count() > Parameters

t : table

The table to count elements in

Table Functions > table.count() > Return Value

Type : integer

The number of values inside the table

Table Functions > table.count() > Example

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


Table Functions > table.deleteAndClear()

A function to delete a deep value in a table and clear the table if it's empty <br>

Table Functions > table.deleteAndClear() > Syntax

table.deleteAndClear(t, keys)

Table Functions > table.deleteAndClear() > Parameters

t : table

The table

keys : any

The keys to deep

Table Functions > table.deleteAndClear() > Return Value

Type : boolean

Returns true if the value was deleted


Table Functions > table.deleteDeepValue()

A function to delete a deep value in a table <br>

Table Functions > table.deleteDeepValue() > Syntax

table.deleteDeepValue(t, keys)

Table Functions > table.deleteDeepValue() > Parameters

t : table

The table

keys : any

The keys to deep

Table Functions > table.deleteDeepValue() > Return Value

Type : table,

Table Functions > table.deleteDeepValue() > Example

local tbl = { a = 1, b = { c = 2, d = 3 } }
table.deleteDeepValue(tbl, "b", "c")
log(tbl)
-- Expected output: {a = 1, b = {d = 3}}

Table Functions > table.doesKeyExist()

A function to know if a deep key exists in a table <br>

Table Functions > table.doesKeyExist() > Syntax

table.doesKeyExist(t, ...)

Table Functions > table.doesKeyExist() > Parameters

t : table

The table to check

... : string

The list of keys to deep

Table Functions > table.doesKeyExist() > Return Value

Type : boolean,

Table Functions > table.doesKeyExist() > Example

local tbl = { a = 1, b = { c = 2 } }
----
-- Simple table
----
print(table.doesKeyExist(tbl, "a")) -- Expected output: true
print(table.doesKeyExist(tbl, "f")) -- Expected output: false
----
-- Deep table
----
print(table.doesKeyExist(tbl, "b", "c")) -- Expected output: true
print(table.doesKeyExist(tbl, "b", "d")) -- Expected output: false
print(table.doesKeyExist(tbl, "b", "c", "d")) -- Expected output: false

Table Functions > table.extract()

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

Table Functions > table.extract() > Syntax

table.extract(t, key)

Table Functions > table.extract() > Parameters

t : table

The table to extract from

key : any

The key to extract

Table Functions > table.extract() > Return Value

Type : any

The extracted value

Table Functions > table.extract() > Example

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


Table Functions > table.filter()

Filters a table based on a callback function. <br>

Table Functions > table.filter() > Syntax

table.filter(t, filterIter, keepKeyAssociation)

Table Functions > table.filter() > 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 <BadgeOptional />

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

Table Functions > table.filter() > Return Value

Type : table

The filtered table

Table Functions > table.filter() > Example

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)
log(tbl2)
-- Expected output : tbl = {b=2}


Table Functions > table.find()

Returns the first element in the table that satisfies the provided function. <br>

Table Functions > table.find() > Syntax

table.find(t, func)

Table Functions > table.find() > 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)

Table Functions > table.find() > Return Value

Type : any,any

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

Table Functions > table.find() > Example

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 Functions > table.getDeep()

A function to get a deep value in a table <br>

Table Functions > table.getDeep() > Syntax

table.getDeep(t, keys)

Table Functions > table.getDeep() > Parameters

t : table

The table

keys : any

The keys to deep

Table Functions > table.getDeep() > Return Value

Type : any,


Table Functions > table.includes()

A function to search a value inside an array <br>

Table Functions > table.includes() > Syntax

table.includes(t, value, fromIndex)

Table Functions > table.includes() > Parameters

t : table

The table

value : any

The value to search

fromIndex : integer <BadgeOptional />

index at which to start searching <br> default: 1

Table Functions > table.includes() > Return Value

Type : integer

Returns the index of the found value or 0 if missing

Table Functions > table.includes() > Example

local tbl = { 1, 2, 3, 4, 5 }
print(table.includes(tbl, 3)) -- Expected output: true
print(table.includes(tbl, 3, 4)) -- Expected output: false because the index of `3` is lower than `fromIndex` (4) value
print(table.includes(tbl, 6)) -- Expected output: false

Table Functions > table.isEgal() > Syntax

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

Table Functions > table.isEgal() > Parameters

table1 : table

First table to compare

table2 : table

Second table to compare

strict : boolean <BadgeOptional />

If all keys should be in both tables <br> default:true

canMissInTable1 : boolean <BadgeOptional />

If table2 keys can miss in table1 <br> default:false

canMissInTable2 : boolean <BadgeOptional />

If table1 keys can miss in table2 <br> default:false

Table Functions > table.isEgal() > Return Value

Type : boolean

Returns true if tables are equal according to specified parameters

Table Functions > table.isEgal() > Example

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 Functions > table.isEmpty() > Syntax

table.isEmpty(t)

Table Functions > table.isEmpty() > Parameters

t : table

The table to check

Table Functions > table.isEmpty() > Return Value

Type : boolean

Returns true if the table is empty

Table Functions > table.isEmpty() > Example

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


Table Functions > table.map()

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

Table Functions > table.map() > Syntax

table.map(t, func)

Table Functions > table.map() > Parameters

t : table

The table to map

func : function|any

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

Table Functions > table.map() > Return Value

Type : table

The new mapped table

Table Functions > table.map() > Example

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


Table Functions > table.merge() > Syntax

table.merge(deepMerge, ...)

Table Functions > table.merge() > Parameters

deepMerge : boolean <BadgeOptional />

Whether to deep merge tables

... : table

The tables to merge

Table Functions > table.merge() > Return Value

Type : table

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

Table Functions > table.merge() > Example

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 Functions > table.mergeAfter()

Merges the values of the second table sequentially into the first table. <br>

Table Functions > table.mergeAfter() > Syntax

table.mergeAfter(...)

Table Functions > table.mergeAfter() > Parameters

... : table

The tables to merge

Table Functions > table.mergeAfter() > Return Value

Type : table

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

Table Functions > table.mergeAfter() > Example

local array1 = { 1, 2, 3 }
local array2 = { 4, 5, 6 }
local result = table.mergeAfter(array1, array2)
log(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)
log(mergeAfterResult)
-- Expected output: {a = 1, b = 2, 1 = 3, 2 = 4}


Table Functions > table.slice()

Get a part of table with stard and end index <br>

Table Functions > table.slice() > Syntax

table.slice(t, s, e)

Table Functions > table.slice() > Parameters

t : table

The table to get from

s : integer <BadgeOptional />

The start intex <br> default:1

e : integer <BadgeOptional />

The end index <br> default:s

Table Functions > table.slice() > Return Value

Type : table

The extracted table

Table Functions > table.slice() > Example

local tbl = { 1, 2, 3, 4, 5 }
local tbl2 = table.slice(tbl, 2, 4)
log(tbl2)
-- Expected output : tbl2 = {2,3,4}

Table Functions > table.upsert()

A function to set/update a value in a table with multiple levels <br>

Table Functions > table.upsert() > Syntax

table.upsert(...)

Table Functions > table.upsert() > Parameters

... : table|string

if the 1st argument is a table, keys will be injected in it. Else, a new table will be created. The last argument is the value to set

Table Functions > table.upsert() > Return Value

Type : table

The new table with multiples table levels

Table Functions > table.upsert() > Example

local tbl = { a = 1, b = 2 }
table.upsert(tbl, "a", 10)
log(tbl)
-- Expected output: {a = 10, b = 2}
table.upsert(tbl, "c", 3)
log(tbl)
-- Expected output: {a = 10, b = 2, c = 3}
Back to documentation