Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tableHelper): add getEntryKeyByValue #47

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/lua/tableHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,21 @@ function tableHelper.isEmpty(inputTable)
return false
end

--- Retrieve a key associated with a specific value within a table.
---
--- @param inputTable table The table to search within.
--- @param searchValue any The value to search for within the table.
--- @return any|false The key associated with the searchValue if found, or false if not found.
HotaruBlaze marked this conversation as resolved.
Show resolved Hide resolved
--- Note: This function is intended to handle situations where the loaded key may not align with the expected key due to the behavior of "cjson."
function tableHelper.getEntryKeyByValue(inputTable, searchValue)
YvanCywan marked this conversation as resolved.
Show resolved Hide resolved
for key, value in pairs(inputTable) do
if key == searchValue or tostring(key) == tostring(searchValue) then
return key
end
end
return false
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