How do i deep search a table?

Hello, I’ve been looking for ways to search through a table to find a value/index and return that table the value/index is in. How would I do that? Here is an example of what I mean…

local tbl = {
	"Abc",
	
	["Pineapple"] = {
		2,
		"Something",
		
		["Cake"] = {
			["Apple"] = 3
		},
	},
};

How would i know “tbl” has “Apple” in it?

1 Like

You can use some recursion:

local function recursive_find(t, key_to_find)
    for key, value in pairs(t) do
        if key == key_to_find then
            return true
        end

        if typeof(value) == "table" then
            return recursive_find(value, key_to_find)
        end
    end
    return false
end

print(recursive_find({
    dog = {
        cat = {
            puppy = {
                kitten = { }
            }
        }
    }
}, "kitten")) -- true
9 Likes