TableUtil
The TableUtil module provides utility functions for Lua tables.
Copy(Table tbl)
¶
Creates and returns a deep copy of the given table.
1 2 |
|
CopyShallow(Table tbl)
¶
Creates and returns a shallow copy of the given table.
1 2 |
|
Sync(Table tbl, Table templateTbl)
¶
Synchronizes a table to the template table. If the table does not have an item that exists within the template, it gets added. If the table has something that the template doesn't have, it gets removed.
1 2 3 4 |
|
Print(Table tbl, String label, Boolean deepPrint)
¶
Prints the table to the output. This is useful for debugging tables.
1 2 |
|
Warning
This method does not detect cyclical tables. Passing a cyclical table will result in a stackoverflow.
FastRemove(Table tbl, Number index)
¶
Removes an item at index
in the table tbl
. Instead of using table.remove
, this simply grabs the last item in the table and places it at the given index and trims off the last item. This is significantly faster than table.remove
because it runs in O(1)
, whereas table.remove
is O(N)
.
1 2 3 |
|
Warning
The order of the table is not preserved using this function. Do not use this if the ordering of your table needs to be maintained.
FastRemoveFirstValue(Table tbl, Variant value)
¶
Performs the FastRemove
operation on the first index that contains value
.
1 2 3 |
|
Map(Table tbl, Function callback)
¶
Constructs a new table by populating it with the results of calling a function on every item in the tbl
table. Useful for restructuring an existing table.
1 2 3 4 5 6 7 8 9 10 11 |
|
Filter(Table tbl, Function callback)
¶
Constructs a new table based on tbl
where it will only include items allowed by the callback
function. Each item in the tbl
table are passed to the callback.
1 2 3 4 5 6 7 8 9 |
|
Reduce(Table tbl, Function callback)
¶
Reduces the tbl
table to a single number. This is useful for tasks such as summing up a table's values. Each item is passed to the callback, as well as an accumulator.
1 2 3 4 5 |
|
Assign(Table target, Table sources...)
¶
Allows the assigning of values from multiple tables into one. The Assign
function is similar to JavaScript's Object.Assign()
function and is useful for things such as composition-designed systems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
IndexOf(Table tbl, Variant item [, Number start])
¶
Returns the index of the given item
in the table. If no item is found, this will return nil
. Optionally, a start
index can be provided.
1 2 3 4 5 |
|
Note
table.find
does the exact same operation, but was introduced after this method was written. For backwards compatibility, this method will continue to exist, but simply points to the table.find
function instead.
Reverse(Table tbl)
¶
Creates a reversed version of the table.
1 2 |
|
Warning
This is a shallow copy.
Shuffle(Table tbl)
¶
Shuffles the array. Internally, this is using the Fisher-Yates algorithm to shuffle around the items.
1 2 3 |
|
Warning
This mutates the table.
IsEmpty(Table tbl)
¶
Returns true
if the table is empty.
1 2 3 4 5 6 |
|
EncodeJSON(Table tbl)
¶
Shortcut for HttpService:JSONEncode()
1 2 3 |
|
DecodeJSON(String json)
¶
Shortcut for HttpService:JSONDecode()
1 2 3 4 5 |
|