This question already has an answer here:
- access parent object in javascript 10 answers
I have the following (nested) object:
obj: { subObj: { foo: 'hello world' } };
Next thing I do is to reference the subobject like this:
var s = obj.subObj;
Now what I would like to do is to get a reference to the object obj
out of the variable s
.
Something like:
var o = s.parent;
Is this somehow possible?
This is an old question but as I came across it looking for an answer I thought I will add my answer to this to help others as soon as they got the same problem.
I have a structure like this:
Currently, by referencing one of the sub nodes I don't know how to get the parent node with it's name value "Main Level".
Now I introduce a recursive function that travels the structure and adds a parent attribute to each node object and fills it with its parent like so.
Then I just call that function and can now get the parent of the current node in this object tree.
If I now have a reference to the seconds sub node of root, I can just call.
and it will output "Main Level".
Hope this helps..
There is a more 'smooth' solution for this :)
Just in keeping the parent value in child attribute
A nested object (child) inside another object (parent) cannot get data directly from its parent.
Have a look on this:
If you ask the main object what its child name is (
main.child.name
) you will get it.Instead you cannot do it vice versa because the child doesn't know who its parent is.
(You can get
main.name
but you won't getmain.child.parent.name
).By the way, a function could be useful to solve this clue.
Let's extend the code above:
Inside the
init
function you can get the parent object simply callingthis
.So we define the
parent
property directly inside thechild
object.Then (optionally) we can remove the
init
method.Finally we give the main object back as output from the
init
function.If you try to get
main.child.parent.name
now you will get it right.It is a little bit tricky but it works fine.
You will need the child to store the parents this variable. As the Parent is the only object that has access to it's this variable it will also need a function that places the this variable into the child's that variable, something like this.
To test this out try to run this in Firefox's Scratchpad, it worked for me.
I have been working on a solution to finding the parent object of the current object for my own pet project. Adding a reference to the parent object within the current object creates a cyclic relationship between the two objects.
Consider -
The variable obj will now look like this -
obj.innerObj.parent.innerObj.parent.innerObj...
This is not good. The only solution that I have found so far is to create a function which iterates over all the properties of the outermost Object until a match is found for the current Object and then that Object is returned.
Example -
Of course, without knowing or having a reference to the outermost object, there is no way to do this. This is not a practical nor is it an efficient solution. I am going to continue to work on this and hopefully find a good answer for this problem.