Lua equivalent to the javascript Array.prototype.includes function, except fromIndex is 1-indexed instead of zero-indexed. Determines whether an array includes a certain value and returns true
or false
.
array
is the array to search. If false
.
value
is the value to be tested. If value
is present in the array, the module will return true
. If value
is missing the module will return false
.
fromIndex
is the optional 1-based index at which to start searching. If fromIndex
is not present, all values in the array will be searched and the array will be treated as a table/associative array (it will be iterated over using
If fromIndex
is present and an integer, the array is assumed to be a conventional array/sequence/list (indexed with consecutive integer keys starting at 1
, and interated over using fromIndex
or higher will be searched.
In the following examples, #array
represents the length of the integer-keyed portion of the array.
fromIndex < 0
it will count back from the end of the array, e.g. a value of -1
will only search the last integer-keyed element in the array. If fromIndex <= (-1 * #array)
, the entire integer-keyed portion of the array will be searched.fromIndex = 0
it will be treated as a 1
and the entire integer-keyed portion of the array will be searched.fromIndex > #array
, the array is not searched and false
is returned.-- These will return trueincludes("b")includes("b", 0)includes("b", 1)includes("b", 2)includes("b", -3)includes("b", -5)includes("b")includes("b", 0)includes("b")
--these will return falseincludes("b","b") -- array is not a tableincludes -- value missingincludes("e") -- "e" is not in arrayincludes("b", 3) -- "b" is before position 3includes("b", 5) -- 5 is larger than #arrayincludes("b", -2) -- "b" is not in the last two positionsincludes("b", 0) -- key 100 is non-consecutiveincludes("b", 0) -- key "second" is not an integer