Is there a way to make a recursive search into a table and return it?

I’m using the Roact module for creating GUIs, but I don’t want to declare everything, just the items which don’t change. Imagine that I have the following table:

{
    ["Frame1"] = {
        ["className"] = "Frame",
        ["properties"] = {--instance properties here},
        ["children"] = {
            ["someChild"] = {
                 ["className"] = "TextLabel",
                 ["properties"] = {--instance properties here},
                 ["children"] = {}
           }
        }
    -- other gui objects w/ the same format...
    }
}

Is there any way to loop through the entire table, and return it as a single table of Roact elements?

I mean, for the recursive function:

Old code New code here


function lookThrough(tabl)
for k,v in pairs(tabl) do
– Do conversion here
if type(v) == “table” then
lookThrough(v)
end
end
end

But, after the conversion, there’s a way to get the whole table? Because when you do lookThrough(), I was looking for a way to join this w/ the main table.

Sorry, I deleted my original post but it got undeleted

Is what you’re trying to do is to iterate over all frames?

I’m not familiar with Roact, you’ll have to add the creation yourself, but here is the function for iterate over all descendants:

function lookThrough(tabl)
    for k,v in pairs(tabl) do
    -- Do conversion here
        if v.children ~= nil then
              lookThrough(v)
        end
    end
end

Is what you’re trying to do is to iterate over all frames?

Yep, but I’m trying to join the objects passed in lookThrough() into the main table, like this:

local convertedTable = {
    ["Frame1"] = Roact.createElement("Frame", {
        -- converted properties here
    }, -- table of all Frame1 children here)
    -- other roact elements with children...
}

I’m trying to figure out how to properly render an object children, and the children children, and so on.

I see, so for each object, you want to create a roact element out of it. Is that it?

Maybe this:

function lookThrough(tabl, parent)
    for k,v in pairs(tabl) do
    local obj = Roact.createElement(v.className, v.properties)
        if v.children ~= nil then
              lookThrough(v, obj)
        end
    Roact.mount(obj, parent)
    end
end
1 Like