JavaScript Objects: Get Parent

Javascript objects: get parent

No. There is no way of knowing which object it came from.

s and obj.subObj both simply have references to the same object.

You could also do:

var obj = { subObj: {foo: 'hello world'} };
var obj2 = {};
obj2.subObj = obj.subObj;
var s = obj.subObj;

You now have three references, obj.subObj, obj2.subObj, and s, to the same object. None of them is special.

Access parent's parent from javascript object

JavaScript does not offer this functionality natively. And I doubt you could even create this type of functionality. For example:

var Bobby = {name: "Bobby"};
var Dad = {name: "Dad", children: [ Bobby ]};
var Mom = {name: "Mom", children: [ Bobby ]};

Who does Bobby belong to?

access parent object in javascript

You can't.

There is no upwards relationship in JavaScript.

Take for example:

var foo = {
bar: [1,2,3]
}

var baz = {};
baz.bar = foo.bar;

The single array object now has two "parents".

What you could do is something like:

var User = function User(name) {
this.name = name;
};

User.prototype = {};
User.prototype.ShowGreetings = function () {
alert(this.name);
};

var user = new User('For Example');
user.ShowGreetings();

Get parent object from key or its value in a nested object

This can be done using a recursive function.


const paths = {
"path": "./parent",
"name": "parent",
"type": "folder",
"children": [
{
"path": "./parent/child1",
"name": "child1",
"type": "folder",
"children": [
{
"path": "./parent/child1/file1",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child1/file2",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
},
{
"path": "./parent/child2",
"name": "child2",
"type": "folder",
"children": [
{
"path": "./parent/child2/file3",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child2/file4",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
}
]
}

const pathtofind = "./parent/child1";

function findChildrenInPath(object, path) {
if (path.startsWith(object.path)) {
if (object.path == path) {
return object.children;
}
else {
for (let child of object.children) {
const result = findChildrenInPath(child, path);
if (result) {
return result;
}
}
}
}
}

const res = findChildrenInPath(paths, pathtofind);
console.log(res);

javascript get parent nested object?

You can't exactly. You have no mean to know in what objects your function exists.

Note that it could be in more than one : you could have written this after your existing code :

var obj2 = {some:obj.subobj3};

So there can't be a unique link (and there is no accessible link) from a property value to the object holding it.

Now, supposing you'd be satisfied with a link made at object creation, you could use a factory to build your object :

obj = (function(){
var parent = {
subobj1: {

},
subobj2: {
func1: function(){

},
func2: function(){

}
},
subobj3: {
func3: function(){

},
func4: function(){
parent.subobj2.func1();
}
}
};
return parent;
})();

Then you can call

obj.subobj3.func4();

Demonstration


EDIT

I see you gave the tag OOP to your question. You should know that the pattern I gave is more frequently used to define modules. OOP in javascript is more often done using new and prototype, in order to enable instances sharing methods and inheritance. As you probably want modules rather than OOP, you seem to be fine, though.

See this introduction.

Get parent in javascript without modifying object

You could search the tree:

 findParent(root, toFind) {
for(const child of root.children || []) {
if(child === toFind){
return root;
}
const result = this.findParent(child, toFind);
if(result){
return result;
}
}
}

That can be used as:

  findParent(original, select)


Related Topics



Leave a reply



Submit