Module:table/getNested
Appearance
- This module lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
local error = error
local select = select
--[==[
Given a table and an arbitrary number of keys, will successively access subtables using each key in turn, returning the value at the final key. For example, if {t} is { {[1] = {[2] = {[3] = "foo"}}}}, {export.getNested(t, 1, 2, 3)} will return {"foo"}.
If no subtable exists for a given key value, returns nil, but will throw an error if a non-table is found at an intermediary key.]==]
return function(...)
local n = select("#", ...)
if n < 2 then
error("Must provide a table and at least one key.")
end
local t, k = ...
for i = 3, n do
local v = t[k]
if v == nil then
return nil
end
t, k = v, select(i, ...)
end
return t[k]
end