ExtendedTables: Enhancing Roblox Tables

- Description -

ExtendedTables is a module that enhances the capabilities of tables with a collection of 16 powerful new functions. Inspired by the functionality of JavaScript Arrays, these functions empower you to perform advanced operations on tables effortlessly. Whether you need to manipulate, filter, or transform table data.

- Installation -

  • Get the module from here
  • Import the module
local table = require(path.to.module)

- Functions -

table.each(table:{any}, callback:(index,value,table) -> ())

Loop through a table.

Example:

local tbl = {"string", true, 123, Vector3.zero}
table.each(tbl, function(index,value,tbl) 
    print(index) -- 1
    print(value) -- "string"
    print(tbl) -- {"string", true, 123, Vector3.zero}
end)

table.toJSON(table:{})

Return table in JSON string format.


table.filter(table:{any}, callback:(value,index,table)->())

This method is used to create a new table with all elements that pass a certain condition. It takes a callback function as an argument, which is executed for each element in the table. The callback function should return true or false to indicate whether the element should be included in the new table.

Read more about filters..

Example:

local numbers = {1,2,3,4,5,6,7,8,9,10}
local evenNumbers = table.filter(numbers, function(value)
   return (value % 2) == 0
end)

print(evenNumbers) -- {2,4,6,8,10}

Another example:

local data = {
	{name="Roblox",cash=2155},
	{name="Synitx",cash=25815},
	{name="builderman",cash=98}
}
local filteredVal = table.filter(data,function(value)
	return value.name:lower() == "synitx"
end) -- Returns { {name="Synitx", cash=25815} }

local info = filteredVal[1]
print(`Name is {info.name} with cash {info.cash}`)
-- Name is Synitx with cash 25815

table.map(table:{any}, callback: (value,key,table) -> ())

Returns a modified table.
Read more about map..

Example:

local numbers = {1,2,3,4,5,6,7,8,9,10}
local doubled = table.map(numbers,function(value)
   return value * 2
end)

print(doubled) -- {2,4,6,8,10,12,14,16,18,20}

table.length(table:{any})

Returns table length.

Example:

print( table.length( {1,2,3,4} ) ) -- 4

table.at(table:{any}, index:number)

Returns the given index’s value.

Example:

local dataTypes = {"string", true, 123, Vector3.zero}

print( table.at(dataTypes,2) ) --  true
print( table.at(dataTypes,3) ) --  123

table.indexOf(table:{any}, value:any)

Returns the given value’s index.

Example:

local dataTypes = {"string", true, 123, Vector3.zero}

print( table.indexOf(dataTypes, "string") ) --  1

table.merge(table:{any}, ... {any} )

Merges table together.

Example:

local numbers = {1,2,3,4,5,6,7,8,9,10}
local a = {15251,26,66,7,{632,36,32,237,27,7,{3266,27,7,47}}}

print( table.merge( {a="string",b={true} }, numbers, a) )
Output


table.keys(table:{any})

Displays all the keys in the given table.

Example:

local data = {
  ["Key1"] = true,
  ["Key2"] = {1,2,3},
  ["Key3"] = 123,
  ["Key4"] = "string"
}

print( table.keys(data) ) -- {"Key1", "Key2" .. etc}

table.values(table:{any})

Displays all the values

local data = {
  ["Key1"] = true,
  ["Key2"] = {1,2,3},
  ["Key3"] = 123,
  ["Key4"] = "string"
}

print( table.values(data) ) -- {true, {1,2,3}, 123, "string"}

table.hasKey(table:{any}, key)

Returns true/false with key’s value.

Example:

local data = {
  ["Key1"] = true,
  ["Key2"] = {1,2,3},
  ["Key3"] = 123,
  ["Key4"] = "string"
}

local exists, value = table.hasKey(data, "Key3")
print(exists, value) -- true, 123

table.hasValue(table:{any}, value:any)

Returns true/false with value’s key.

Example:

local data = {
  ["Key1"] = true,
  ["Key2"] = {1,2,3},
  ["Key3"] = 123,
  ["Key4"] = "string"
}

local exists, key = table.hasValue(data, 123)
print(exists, key) -- true, "Key3"

table.getType(table:{any}, type)

Returns values of a give type.
If value is more than 1 then it will return it in a table.

Example:

local data = {
	["Key1"] = true,
	["Key2"] = {1,2,3},
	["Key3"] = 123,
	["Key4"] = "hi",
	["Key5"] = 596,
    ["Key6"] = false
}

print(table.getType(data,"number")) -- {123, 596}
print(table.getType(data,"boolean")) -- {true, false}
print(table.getType(data,"string")) -- hi

table.slice(table:{any}, startIndex:number?, endIndex:number?)

Returns a specific portion of a table.
Read more about slice..

Example:

local data = {
	"dog",
	"cat",
	"cow",
	"bird",
	"whale",
	"fish",
	"flamingo"
}

print(table.slice(data,3)) --  starts from index of 3 which is cow
-- output: {"cow","bird","whale","fish","flamingo"}

print(table.slice(data,3,4)) -- starts from index of 3 and ends at index 4
-- output: {"cow","bird"}

table.flat(table:{any})

table.flat is a function that recursively flattens a table by merging its nested tables into a single level.

Read more about flat..

Example:

local a = {15251,26,66,7,{632,36,32,237,27,7,{3266,27,7,47}}}
print( table.flat(a) )
Output

image


table.valueAmount(table:{})

Returns total amount of values in a table (including nested tables)

Example:

local a = {15251,26,66,7,{632,36,32,237,27,7,{3266,27,7,47}}}
print( table.valueAmount(a) ) -- 14

GitHub Button SOCIAL Release Badge VERSION

Check out my other posts

Advanced Webhooks || Easily interact with discord webhooks

A guide to decorate your posts in forum!

Quick Cache | Store your cache data and track threads!

Decorate+ || Plugin for Terrain Material Replacement Under A Part

13 Likes

That shall save a lot of for loops. I find it useful in some cases.

Nice one.

2 Likes

A bunch of these kind of already exist, but the others look pretty useful.

1 Like

is there a feature for getting a length of a dictionary (not an array) ?, if not, it would be nice to have that feature though.

1 Like

Like a feature that shows the total amount of nested tables?

1 Like

No not really, just find out how many “children” there are in a table.

actually, now that i’ve read the functions again, I can just do #table.keys(data) lol thanks

2 Likes

Do you think adding tuples to this module would be useful?

Don’t know what tuples are?

See tuples in roblox

  • Yes
  • No

0 voters

If you have any suggestions then please let me know!

1 Like

Personally, I prefer TableUtil by sleitnick as it has most of the functionality I was hoping for.

2 Likes

Oh, I wasn’t aware of that module.
But yeah, it’s up to you which module to use. I simply created this module for fun.