How to get parent of element in table?

I have this in encode : [[{“F1”:[{“R1”:[]},{“R2”:[{“Decorations”:3,“TV”:1,“Toilet”:2}]}]},{“F2”:[{“R1”:[{“Decorations”:3,“TV”:1,“Toilet”:2}]}]}]] . Or this in script:

local Hotel2 = {}

table.insert(Hotel2,{F1 = {}})
table.insert(Hotel2[1].F1,{["R1"] = {}})
table.insert(Hotel2[1].F1,{["R2"] = {}})
table.insert(Hotel2[1].F1[2].R2,{TV = 1,Toilet = 2,Decorations = 3})
table.insert(Hotel2,{["F2"] = {}})
table.insert(Hotel2[2].F2,{["R1"] = {}})
table.insert(Hotel2[2].F2[1].R1,{TV = 1,Toilet = 2,Decorations = 3})

So i want get Parent of element, like :

print(Hotel2[2].F2[1].R1.Parent) -- I want get element parent (F2)

I know that i can use metatables,but i haven’t any ideas about solution.

Are you storing objects in that table, or names? This is a headache to look at. If you are not storing objects, you cannot get their parents

Oof, if this is your layout you should probably look into object-oriented programming tutorials for Lua, because that could help tremendously. I can give you an example of creating a table-based object system that would be lightweight but work quite well in this circumstance.

Yes,you can. Also i know metatables 50/50,but i haven’t ideas about solution.

F2 = {["R2"] = {}}
_1 = F2
_2 = {}
_3 = 0

for _4, _5 in pairs(_1) do 
    _3 += 1
    _2[_3] = {_4, _5}
end

n = 1 (first key of F2)
_2[n][1] = "R2"
_2[n][2] = {}

1 will be the key (“Parent”) and 2 will be the value.

1 Like