Skip to content

Commit fea218d

Browse files
authored
feat(tableHelper): add getEntryInTableByValue (#47)
1 parent d930680 commit fea218d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/lua/tableHelper.lua

+15
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ function tableHelper.isEmpty(inputTable)
400400
return false
401401
end
402402

403+
--- Retrieve a key associated with a specific value within a table.
404+
---
405+
--- @param inputTable table The table to search within.
406+
--- @param searchValue string|number The value to search for within the table.
407+
--- @return string|number|false The key associated with the searchValue if found, or false if not found.
408+
--- 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."
409+
function tableHelper.getEntryKeyByValue(inputTable, searchValue)
410+
for key, value in pairs(inputTable) do
411+
if key == searchValue or tostring(key) == tostring(searchValue) then
412+
return key
413+
end
414+
end
415+
return false
416+
end
417+
403418
-- Check whether the table is an array with only consecutive numerical keys,
404419
-- i.e. without any gaps between keys
405420
-- Based on http://stackoverflow.com/a/6080274

0 commit comments

Comments
 (0)