Array
An array is a table consisting of index-value pairs. You don't need to manually specify the indices when you create an array.
local array = {
"hello",
"world",
}
Aliases
List
Functions
concat
Array.
concat
(
...:
...any
--
The arrays to concatenate.
) →
{
T
}
--
The concatenated array.
Joins multiple arrays together into a single array.
Aliases
join
, merge
local table1 = { 1, 2, 3 }
local table2 = { 4, 5, 6 }
local new = Concat(table1, table2) -- { 1, 2, 3, 4, 5, 6 }
sort
Array.
sort
(
array:
{
T
}
,
--
The array to sort.
comparator?:
(
a:
T
,
b:
T
)
→
boolean
--
The comparator function.
) →
{
T
}
--
The sorted array.
Sorts an array.
local array = { "a", "b", "c", "d", "e" }
local new = Sort(array, function(a, b)
return a > b
end) -- { "e", "d", "c", "b", "a" }
pop
Array.
pop
(
array:
{
T
}
,
--
The array to pop an element from.
count?:
number
=
1
--
The number of elements to pop.
) →
{
T
}
--
An array with the popped elements removed.
Removes an element from the end of the array, and returns the array with the popped elements removed.
local array = { 1, 2, 3 }
local new = Pop(array) -- { 1, 2 }
local new = Pop(array, 2) -- { 1 }
copy
Array.
copy
(
array:
{
T
}
--
The array to copy.
) →
{
T
}
--
The copied array.
Copies an array.
local array = { 1, 2, 3 }
local new = Copy(array) -- { 1, 2, 3 }
print(new == array) -- false
push
Array.
push
(
array:
{
T
}
,
--
The array to push an element to.
...:
...T
--
The elements to push.
) →
{
T
}
--
The array with the pushed elements.
Adds elements to the end of the array.
Aliases
append
local array = { 1, 2, 3 }
local new = Push(array, 4, 5, 6) -- { 1, 2, 3, 4, 5, 6 }
update
Array.
update
(
array:
{
T
}
,
--
The array to update.
index:
number
,
--
The index to update.
updater?:
(
value:
T
,
index:
number
)
→
T
,
--
The updater function.
callback?:
(
index:
number
)
→
T
--
The callback function.
) →
{
T
}
--
The updated array.
Updates an array at the given index. If the value at the given index does
not exist, callback
will be called, and its return value will be used
as the value at the given index.
local array = { 1, 2, 3 }
local new = Update(array, 2, function(value)
return value + 1
end) -- { 2, 3, 3 }
local new = Update(array, 4, function(value)
return value + 1
end, function(value)
return 10
end) -- { 1, 2, 3, 10 }
find
Array.
find
(
array:
{
T
}
,
--
The array to search.
value?:
any
,
--
The value to search for.
from?:
number
--
The index to start searching from.
) →
number?
--
The index of the first item in the array that matches the value.
Finds the index of the first item in the array that matches the value. This is
mostly a wrapper around table.find
, with the ability to specify a negative
number as the start index (to search relative to the end of the array).
Aliases
indexOf
local array = { "hello", "world", "hello" }
local index = Find(array, "hello") -- 1
local index = Find(array, "hello", 2) -- 3
first
Array.
first
(
array:
{
T
}
--
The array to get the first item from.
) →
T
--
The first item in the array.
Gets the first item in the array.
local array = { 1, 2, 3 }
local value = First(array) -- 1
map
Array.
map
(
array:
{
T
}
,
--
The array to map.
mapper:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
U?
--
The mapper function.
) →
{
U
}
--
The mapped array.
Maps the array using the mapper function.
local array = { 1, 2, 3 }
local new = Map(array, function(value, index)
return value * 2
end) -- { 2, 4, 6 }
is
Array.
is
(
object:
any
--
The object to check.
) →
boolean
--
Whether the object is an array.
Checks if the given object is an array.
local array = { 1, 2, 3 }
local dictionary = { hello = "world" }
local mixed = { 1, 2, hello = "world" }
Array.is(array) -- true
Array.is(dictionary) -- false
Array.is(mixed) -- false
reverse
Array.
reverse
(
array:
{
T
}
--
The array to reverse.
) →
{
T
}
--
The reversed array.
Reverses the order of the items in an array.
local array = { 1, 2, 3 }
local new = Reverse(array) -- { 3, 2, 1 }
zip
Array.
zip
(
...:
{
any
}
--
The arrays to zip together.
) →
{
any
}
--
The zipped array.
Zips multiple arrays together into a single array.
local table1 = { 1, 2, 3 }
local table2 = { "hello", "world", "goodbye" }
local new = Zip(table1, table2) -- { { 1, "hello" }, { 2, "world" }, { 3, "goodbye" } }
flatten
Array.
flatten
(
array:
{
T
}
,
--
The array to flatten.
depth?:
number
--
The depth to flatten the array to.
) →
{
T
}
--
The flattened array.
Flattens an array. If depth is not specified, it will flatten the array as far as it can go.
local array = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, { 8, 9 } },
}
local new = Flatten(array) -- { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
local new = Flatten(array, 1) -- { 1, 2, 3, 4, 5, 6, 7, { 8, 9 } }
includes
Array.
includes
(
array:
{
T
}
,
--
The array to search.
value:
any
,
--
The value to search for.
from?:
number
--
The index to start searching from.
) →
boolean
--
Whether the array contains the value.
Checks whether the array contains the value. This is a wrapper
around Find
.
Aliases
contains
, has
local array = { "hello", "world", "goodbye" }
local value = Includes(array, "hello") -- true
local value = Includes(array, "sift") -- false
local value = Includes(array, "hello", 2) -- false
removeValues
Array.
removeValues
(
array:
{
T
}
,
--
The array to remove values from.
...:
T
--
The values to remove.
) →
{
T
}
--
The array with the values removed.
Removes values from an array.
local array = { "a", "b", "c", "c", "d", "e" }
local new = RemoveValues(array, "c", "d") -- { "a", "b", "e" }
create
Array.
create
(
length:
number
,
--
The length of the array to create.
value?:
T
--
The value to fill the array with.
) →
{
T
}
--
The created array.
Creates an array of the given length, filled with the given value.
This is just a wrapper around table.create
.
local array = Create(3, "Hello")
print(array) -- { "Hello", "Hello", "Hello" }
splice
Array.
splice
(
array:
{
T
}
,
--
The array to splice.
start?:
number
,
--
The index to start splicing at (can be negative).
end?:
number
,
--
The index to end splicing at (can be negative).
...:
...T
--
The values to insert.
) →
{
T
}
--
The spliced array.
Splices an array.
local array = { 1, 2, 3, 4, 5 }
local new = Splice(array, 3, 4, 6, 7) -- { 1, 2, 6, 7, 4, 5 }
local new = Splice(array, -1, 0, 6, 7) -- { 1, 2, 3, 4, 6, 7 }
local new = Splice(array, 4, -1, 6, 7) -- { 1, 2, 3, 6, 7, 5 }
difference
Returns an array of values that are in the first array, but not in the other arrays.
local array1 = { "hello", "world" }
local array2 = { "cat", "dog", "hello" }
local difference = Difference(array1, array2) -- { "world" }
last
Array.
last
(
array:
{
T
}
--
The array to get the last element of.
) →
T
--
The last element of the array.
Gets the last element of the array.
local array = { 1, 2, 3 }
local value = Last(array) -- 3
removeIndex
Array.
removeIndex
(
array:
{
T
}
,
--
The array to remove the value from.
index:
number
--
The index to remove the value from (can be negative).
) →
{
T
}
--
The array with the value removed.
Removes a value from an array at the given index.
local array = { 1, 2, 3 }
local new = RemoveIndex(array, 1) -- { 2, 3 }
local new = RemoveIndex(array, -1) -- { 1, 3 }
toSet
Converts an array to a set.
local array = { "a", "b", "b", "c", "d" }
local set = ToSet(array) -- { a = true, b = true, c = true, d = true }
removeValue
Array.
removeValue
(
array:
{
T
}
,
--
The array to remove the value from.
value:
T
--
The value to remove.
) →
{
T
}
--
The array with the value removed.
Removes a value from an array.
local array = { 1, 2, 3 }
local new = RemoveValue(array, 2) -- { 1, 3 }
findWhere
Array.
findWhere
(
array:
{
T
}
,
--
The array to search.
predicate:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
,
--
The predicate to use to check the array.
from?:
number
--
The index to start searching from.
) →
number
--
The index of the first item in the array that matches the predicate.
Finds the index of the first item in the array that passes the predicate.
local array = { 1, 2, 3 }
local index = FindWhere(array, function(item, index)
return item > 1
end) -- 2
reduceRight
Array.
reduceRight
(
array:
{
T
}
,
--
The array to reduce.
reducer:
(
accumulator:
U
,
value:
T
,
index:
number
,
array:
{
T
}
)
→
U
,
--
The reducer to use.
initialReduction?:
U
=
{
T
}
[
#
{
T
}
]
--
The initial accumulator value.
) →
U
--
The final accumulator value.
Reduces the array using the given reducer and initial accumulator value,
starting from the end of the array. If no initialReduction
value is given,
the last item in the array is used.
local array = { 1, 2, 3 }
local value = ReduceRight(array, function(accumulator, item, index)
return accumulator - item
end) -- 0
local value = ReduceRight(array, function(accumulator, item, index)
table.insert(accumulator, item)
return accumulator
end, {}) -- { 3, 2, 1 }
at
Array.
at
(
array:
{
T
}
,
--
The array to get the value from.
index:
number
--
The index to get the value from (can be negative).
) →
T
--
The value at the given index.
Gets a value from an array at the given index.
local array = { 1, 2, 3 }
local value = At(array, 1) -- 1
local value = At(array, 0) -- 3
filter
Array.
filter
(
array:
{
T
}
,
--
The array to filter.
filterer?:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
--
The callback to use to filter the array.
) →
{
T
}
--
The filtered array.
Filters an array using a filterer callback. Any items that do not pass the filterer will be removed from the array.
If no filterer is provided, all items will be kept.
local array = { 1, 2, 3 }
local result = Filter(array, function(item, index)
return item > 1
end) -- { 2, 3 }
every
Array.
every
(
array:
{
T
}
,
--
The array to check.
predicate:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
--
The predicate to use to check the array.
) →
boolean
--
Whether every item in the array passes the predicate.
Checks whether every item in the array passes the predicate.
local array = { 1, 2, 3 }
local value = Every(array, function(item, index)
return item > 0
end) -- true
local value = Every(array, function(item, index)
return item > 1
end) -- false
copyDeep
Array.
copyDeep
(
array:
{
T
}
--
The array to copy.
) →
{
T
}
--
The copied array.
Copies an array, with deep copies of all nested arrays.
local array = { 1, 2, 3, { 4, 5 } }
local result = CopyDeep(array) -- { 1, 2, 3, { 4, 5 } }
print(result == array) -- false
print(result[4] == array[4]) -- false
unshift
Array.
unshift
(
array:
{
T
}
,
--
The array to insert the values to.
...:
...T
--
The values to insert.
) →
{
T
}
--
The array with the values inserted.
Inserts values to the beginning of an array.
Aliases
prepend
local array = { 1, 2, 3 }
local new = Unshift(array, 4, 5) -- { 4, 5, 1, 2, 3 }
shuffle
Array.
shuffle
(
array:
{
T
}
--
The array to shuffle.
) →
{
T
}
--
The shuffled array.
Randomises the order of the items in an array.
local array = { 1, 2, 3 }
local new = Shuffle(array) -- { 2, 3, 1 }
equalsDeep
Array.
equalsDeep
(
...:
...
{
any
}
--
The arrays to compare.
) →
boolean
--
Whether the arrays are equal.
Compares two arrays for equality using deep comparison.
local array = { 1, 2, 3, { 4, 5 } }
local other = { 1, 2, 3, { 4, 5 } }
local value = EqualsDeep(array, other) -- true
local value = EqualsDeep(array, other, { 1, 2, 3, { 4, 5 } }) -- true
local value = EqualsDeep(array, other, { 1, 2, 3, { 4, 6 } }) -- false
freeze
Array.
freeze
(
array:
{
T
}
--
The array to freeze.
) →
{
T
}
--
The frozen array.
Freezes the top level of the array, making it read-only.
local array = { 1, 2, 3, { 4, 5, 6 } }
local new = Freeze(array)
new[1] = 4 -- error!
new[4][1] = 7 -- still works!
removeIndices
Array.
removeIndices
(
array:
{
T
}
,
--
The array to remove the indices from.
...:
...number
--
The indices to remove the values from (can be negative).
) →
{
T
}
--
The array with the values removed.
Removes values from an array at the given indices.
local array = { 1, 2, 3 }
local new = RemoveIndices(array, 1, 2) -- { 3 }
local new = RemoveIndices(array, 0, -1) -- { 1 }
set
Array.
set
(
array:
{
T
}
,
--
The array to set the value on.
index:
number
,
--
The index to set the value at (can be negative).
value:
T
--
The value to set.
) →
{
T
}
--
The array with the value set.
Sets a value on an array at the given index.
local array = { 1, 2, 3 }
local new = Set(array, 2, 4) -- { 1, 4, 3 }
local new = Set(array, -1, 4) -- { 1, 2, 4 }
count
Array.
count
(
array:
{
T
}
,
--
The array to count the number of items in.
predicate?:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
--
The predicate to use to filter the array.
) →
number
--
The number of items in the array.
Counts the number of items in an array.
local array = { 1, 2, 3 }
local value = Count(array) -- 3
local value = Count(array, function(item, index)
return item == 2
end) -- 1
shift
Array.
shift
(
array:
{
T
}
,
--
The array to shift.
count?:
number
--
The number of items to shift.
) →
{
T
}
--
The shifted array.
Removes the first item from an array and returns the array with the item removed.
local array = { 1, 2, 3 }
local new = Shift(array) -- { 2, 3 }
local new = Shift(array, 2) -- { 3 }
findWhereLast
Array.
findWhereLast
(
array:
{
T
}
,
--
The array to search.
predicate:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
,
--
The predicate to use to check the array.
from?:
number
--
The index to start searching from.
) →
number
--
The index of the last item in the array that matches the predicate.
Finds the index of the last item in the array that passes the predicate.
local array = { "hello", "world", "hello" }
local index = FindWhereLast(array, function(item, index)
return item == "hello"
end) -- 3
local index = FindWhereLast(array, function(item, index)
return item == "hello"
end, 2) -- 1
reduce
Array.
reduce
(
array:
{
T
}
,
--
The array to reduce.
reducer:
(
accumulator:
U
,
value:
T
,
index:
number
,
array:
{
T
}
)
→
U
,
--
The reducer to use.
initialReduction?:
U
=
{
T
}
[
1
]
--
The initial accumulator value.
) →
U
--
The final accumulator value.
Reduces the array using the given reducer and initial accumulator value.
If no initialReduction
value is given, the first item in the array is used.
local array = { 1, 2, 3 }
local value = Reduce(array, function(accumulator, item, index)
return accumulator - item
end) -- -4
local value = Reduce(array, function(accumulator, item, index)
table.insert(accumulator, item)
return accumulator
end, {}) -- { 1, 2, 3 }
equals
Array.
equals
(
...:
...
{
any
}
--
The arrays to compare.
) →
boolean
--
Whether the arrays are equal.
Compares two arrays for equality.
local array = { 1, 2, 3 }
local other = { 1, 2, 3 }
local value = Equals(array, other) -- true
local value = Equals(array, other, { 1, 2, 3 }) -- true
local value = Equals(array, other, { 1, 2, 4 }) -- false
concatDeep
Array.
concatDeep
(
...:
...any
--
The arrays to concatenate.
) →
{
T
}
--
The concatenated array.
Joins multiple arrays together into a single array, with deep copies of all nested arrays.
Aliases
joinDeep
, mergeDeep
local table1 = { 1, 2, { 3, 4 } }
local table2 = { 5, 6, { 7, 8 } }
local new = ConcatDeep(table1, table2) -- { 1, 2, { 3, 4 }, 5, 6, { 7, 8 } }
findLast
Array.
findLast
(
array:
{
T
}
,
--
The array to search.
value?:
any
,
--
The value to search for.
from?:
number
--
The index to start searching from.
) →
number?
--
The index of the last item in the array that matches the value.
Finds the index of the last item in the array that matches the value.
local array = { "hello", "world", "hello" }
local index = FindLast(array, "hello") -- 3
local index = FindLast(array, "hello", 2) -- 1
slice
Array.
slice
(
array:
{
T
}
,
--
The array to slice.
from?:
number
,
--
The index to start from (can be negative).
to?:
number
--
The index to end at (can be negative).
) →
{
T
}
--
The sliced array.
Slices an array.
local array = { 1, 2, 3, 4, 5 }
local new = Slice(array, 2, 3) -- { 2, 3 }
local new = Slice(array, -2, -1) -- { 3, 4 }
local new = Slice(array, 3) -- { 3, 4, 5 }
zipAll
Array.
zipAll
(
...:
...
{
any
}
--
The arrays to zip.
) →
{
any
}
--
The zipped array.
Zips multiple arrays together into a single array, filling
in missing values with None
.
local table1 = { 1, 2, 3, 4 }
local table2 = { "hello", "world", "goodbye" }
local new = ZipAll(table1, table2) -- { { 1, "hello" }, { 2, "world" }, { 3, "goodbye" }, { 4, None } }
differenceSymmetric
Returns an array of values that are in the first array, but not in the other arrays, and vice versa.
local array1 = { "hello", "world" }
local array2 = { "cat", "dog", "hello" }
local difference = DifferenceSymmetric(array1, array2) -- { "world", "cat", "dog" }
some
Array.
some
(
array:
{
T
}
,
--
The array to check.
predicate:
(
value:
T
,
index:
number
,
array:
{
T
}
)
→
any
--
The predicate to use to check the array.
) →
boolean
--
Whether some item in the array passes the predicate.
Checks whether some item in the array passes the predicate.
local array = { 1, 2, 3 }
local value = Some(array, function(item, index)
return item > 1
end) -- true
local value = Some(array, function(item, index)
return item > 3
end) -- false
insert
Array.
insert
(
array:
{
T
}
,
--
The array to insert the value into.
index:
number
,
--
The index to insert the value at (can be negative).
values:
...T
--
The values to insert.
) →
{
T
}
--
The array with the value inserted.
Inserts the given values into an array at the given index, shifting all values after it to the right. If the index is negative (or 0), it is counted from the end of the array.
If the index to insert at is out of range, the array is not modified.
local array = { 1, 2, 3 }
local newArray = Insert(array, 2, 4) -- { 1, 4, 2, 3 }
freezeDeep
Array.
freezeDeep
(
array:
{
T
}
--
The array to freeze.
) →
{
T
}
--
The frozen array.
Freezes the entire array, making it read-only, including all nested arrays.
local array = { 1, 2, 3, { 4, 5, 6 } }
local new = FreezeDeep(array)
new[1] = 4 -- error!
new[4][1] = 7 -- error!