Module:TableTools: Difference between revisions

m 1 revision imported: Content from Template:Location map+ on Wikipedia
m 1 revision imported: Content from Template:Location map+ on Wikipedia
 
(One intermediate revision by one other user not shown)
Line 460: Line 460:
-- inArray
-- inArray
--
--
-- Returns true if valueToFind is a member of the array, and false otherwise.
-- Returns true if searchElement is a member of the array, and false otherwise.
-- Equivalent to JavaScript array.includes(searchElement) or
-- array.includes(searchElement, fromIndex), except fromIndex is 1 indexed
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
function p.inArray(arr, valueToFind)
function p.inArray(array, searchElement, fromIndex)
checkType("inArray", 1, arr, "table")
checkType("inArray", 1, array, "table")
-- if valueToFind is nil, error?
-- if searchElement is nil, error?


for _, v in ipairs(arr) do
fromIndex = tonumber(fromIndex)
if v == valueToFind then
if fromIndex then
return true
if (fromIndex < 0) then
fromIndex = #array + fromIndex + 1
end
if fromIndex < 1 then fromIndex = 1 end
for _, v in ipairs({unpack(array, fromIndex)}) do
if v == searchElement then
return true
end
end
else
for _, v in pairs(array) do
if v == searchElement then
return true
end
end
end
end
end