Skip to content

Commit

Permalink
feat(tableHelper): add getEntryKeyByValue
Browse files Browse the repository at this point in the history
  • Loading branch information
HotaruBlaze committed Oct 22, 2023
1 parent d930680 commit c6a6d04
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/lua/tableHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ function tableHelper.isEmpty(inputTable)
return false
end

-- Retrieve a key associated with a specific value within a table.
-- Note: This function is intended to handle situations where the loaded value may not align with the expected format due to the behavior of "cjson."
function tableHelper.getEntryKeyByValue(inputTable, searchValue)
for key, value in pairs(inputTable) do
if key == searchValue then
return key
elseif tostring(key) == tostring(searchValue) then
return key
end
end
return nil
end

-- Check whether the table is an array with only consecutive numerical keys,
-- i.e. without any gaps between keys
-- Based on http://stackoverflow.com/a/6080274
Expand Down

0 comments on commit c6a6d04

Please sign in to comment.