array.lua
array.chunk(obj:table,size:number):table
Returns a new array-like table with elements splitted into groups of length of `size`
array.chunk({ 'a', 'b', 'c', 'd', 'e', 'f', 'g' }, 3)
-- { { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g' } }
array.concat(obj:table,obj2:table):table
Return a new table joining all values from the two tables passed by parameter
array.concat({ 1, 2, 3 }, { 4, 5, 6 })
-- { 1, 2, 3, 4, 5, 6 }
array.counter(obj:table):table
and value represent the number of times the same item was found in the list
array.counter({ 'a', 'b', 'a', 'a', 'c', 'b' })
-- { a = 3, b = 2, c = 1 }
array.deep_copy(value:*):table
Return a deep copy of the table passed as parameter
local object = { { a = 1 }, { b = 2 } }
local deep = array.deep_copy(object)
print(deep[1] == object[1])
-- true
array.diff(obj:table,obj2:table):table
Return a new table with the items which exist only in the first table
local diff = array.counter({ 'a', 'b', 'c'}, { 'a', 'c', 'e' })
print(diff)
-- { 'b', 'e' }
array.each(obj:table,callback:function):void
Executes callback once for each table element
array.each({ 'a' = 1, 'b' = 2 }, function(value, key)
print(key)
print(value)
end)
array.every(obj:table,obj2:table):table
Run a predicate on each value. If the predicate evaluates to any false value, this function will immediately return false; otherwise, it returns true.
array.every({ 10, 20 }, function(value)
return value >= 10
end)
-- true
array.fill(value:*,start_or_finish:number,finish:number):table
Creates a table filling all the elements from a start index (default one) to an end index with a default value
array.fill('a', 3)
-- { 'a', 'a', 'a' }
array.filter(obj:table,callback:function):*
Create a new table containing all elements that pass truth test
local result = array.filter({ 15, 10, 5, 3, 20 }, function(value)
return value >= 10
end
print(result)
-- { 15, 10, 20 }
array.first(obj:table):*
Return first element from the table
array.first({ 'a', 'b', 'c', 'd' })
-- 'a'
array.flat(obj:table, depth: number):table
Create a new table with the sub-table elements concatenated into it up to the specific depth.
local result = array.flat({ 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }, 1)
print(result)
-- { 'a', 'b', 'c', 'd', 'e', 'f', { 'g', 'h' }, 'i', 'j' }
array.flat_map(obj:table, callback: function):table
Returns a new array-like table by applying a given callback to each element of the table, and then flattening the result by one level.
local result = array.flat_map({ 1, 2, 3 }, function(value)
return { value, value * 2 }
end)
print(result)
-- { 1, 2, 2, 4, 3, 6 }
array.from_pairs(obj1:table,obj2:table):table
Return a table composed from key-value pairs
local result = array.from_pairs({ {'a', 1}, {'b', 2} })
print(result)
-- { a = 1, b = 2 }
array.group_by(obj:table,callback:function):table
Returns a new table composed by keys created from the results of running each element through `callback`
local result = array.group_by({ 6.1, 4.1, 6.3, 4.4, 5.1 }, function(item)
return math.floor(item)
end)
print(result)
-- { [4] = { 4.1, 4.4 }, [6] = { 6.1, 6.3 }, [5] = { 5.1 } }
array.includes(obj:table,value:*):boolean
Return true whether table includes the values, otherwise it returns false
array.includes({ 'a', 'b', 'c' }, 'a')
-- true
array.index_of(obj:table,value:*):number
Return the index at which value can be found or -1 in case value is not present
array.index_of({ 'a', 'b', 'c' }, 'b')
-- 2
array.intersect(obj1:table,obj2:table):table
Return a new table with the values that exist in both tables
array.intersect({ 1, 2, 3 }, { 2, 3 })
-- { 2, 3 }
array.is_array(obj:table):boolean
Verify if table object works as an array
array.is_array({ 'a' = 1 })
-- false
array.is_empty(obj:table):boolean
Check if parameter is an empty table
array.is_empty({})
-- true
array.key_by(obj:table, callback:function):table
Creates a new table composed of keys generated from the results of running each element of the given table through the given callback
result = array.key_by({ { id = 1, name = 'Evandro' }, { id = 2, name = 'John' } }, function(item)
return item.id
end)
print(result)
-- { [1] = { id = 1, name = 'Evandro' }, [2] = { id = 2, name = 'John' } }
array.last(obj:table):*
Return last element from the table
array.last({ 'a', 'b', 'c', 'd' })
-- 'd'
array.map(obj:table,callback:function):*
Create a new table of values by mapping each value in table through a transformation function
local result = array.map({ 1, 2, 3 }, function(value)
return value * 2
end)
print(result)
-- { 2, 4, 6 }
array.max(obj:table):*
Return maximum value from the table
array.max({ 20, 22, 1, 3, 42, 30 })
-- 42
array.min(obj:table):*
Return minimum value from the table
array.min({ 20, 22, 1, 3, 42, 30 })
-- 1
array.permutation(obj:table):table
Creates a new table returning all permutations of the length of the elements of the given table
array.permutation({ 'lua', 'javascript', 'ruby' })
--{
-- { 'lua', 'javascript', 'ruby' },
-- { 'javascript', 'lua', 'ruby' },
-- { 'javascript', 'ruby', 'lua' },
-- { 'lua', 'ruby', 'javascript' },
-- { 'ruby', 'lua', 'javascript' },
-- { 'ruby', 'javascript', 'lua' }
--}
array.random(obj:table):*
Returns a value from a random key of the given array
array.random({ 20, 22, 1, 3, 42, 30 })
array.reduce(obj:table,callback:function,memo:table):*
Applies a function against an accumulator and each value of the table to reduce it to a single value
local result = array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
return memo .. value
end)
print(result)
-- 'abcde'
array.reduce_right(obj:table,callback:function,memo:table):*
This function is like reduce except that it interates over table's elements from right to left
local result = array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
return memo .. value
end)
print(result)
-- 'abcde'
array.remove(obj:table,callback:function):table
and return a new table with the removed items
array.reverse(obj:table):table
Create a new table with reverse values
array.reverse_each(obj:table,callback:function):void
Executes callback once for each table element in reverse order
array.shallow_copy(obj:table):table
return a shallow copy of the table passed as parameter
array.slice(obj:table,start:number,finish:number):boolean
Return a shallow copy of a portion of a table into a new table
array.some(obj:table,callback:function):boolean
Tests if at least one element in the table passes the test implemented by the callback
array.sum(obj:table,callback:function):number
Return the sum of the values in table
array.uniq(obj:table):table
Create a new table, removing duplicates values
array.without(obj:table,values:table):table
Return a copy of the table with all instances of the values removed
array.zip(obj:table,obj2:table):table
Return a table of the two supplied by pairing up equally-positioned elements from both tables