2

My problem . I have an object like this:

t.mainobject = {
    prop1: "",
    prop2: "",
    id: "1",
    prop3: [
        child1: {
            id: "111"
        },
        child2: {
            id: "112"
        }
    ]
};

only child1/child2 is visible to me whereas I donot know its parent object . I tried using underscore but have not found any method which can provide me the parent object .

thanks in advance for any advice/suggestion.

edit1:
all my child objects were created from multiple object so it end up as a flatten object.

like:

t.mainobject = {
    prop1: "",
    prop2: "",
    id: "1",
    prop3: [
        child1: {
            id: "111"
        },
        child2: {
            id: "112"
        }
    ]
};


t.mainobject1 = {
    prop1: "",
    prop2: "",
    id: "2",
    prop3: [
        child1: {
            id: "211"
        },
        child2: {
            id: "212"
        }
    ]
};

t.mainobject3 = {
    prop1: "",
    prop2: "",
    id: "1",
    prop3: [
        child1: {
            id: "311"
        },
        child2: {
            id: "312"
        }
    ]
};

Now I ended up with an array with :

[{id:"111"},{id:"112"}, {id:"211"},{id:"212"}, {id:"311"},{id:"312"}]

Now how can I able to get the parent object containing the child object using the id .

edit 2:

my solution :

var parent = _.filter(allParent, function(r){
            for(var i=0; i<r.posts.length; i++){
                if(r.posts[i].id === allChildPosts[index].id){
                    return r.id;
                }
            }
        });

it return me the parent object and now I can easily access any property.

user429035
  • 411
  • 1
  • 5
  • 14
  • 7
    What does *"only child1/child2 is visible to me"* mean exactly? There is no such concept as a "parent object" in JavaScript. The relationship between an object and its properties is unidirectional, unless you explicitly set up the inverse relationship. – Felix Kling Sep 03 '14 at 22:33
  • 1
    This has nothing to do with `underscore.js` tag, right? – LcSalazar Sep 03 '14 at 22:38
  • @FelixKling hope my edit summary helps BTW what do you mean by no concept of "parent object" this is a generic term nothing related with javascript or any language . – user429035 Sep 03 '14 at 22:44
  • @LcSalazar correct nothing to do with underscore.js . I am trying to see if i can leverage the library to get what i am looking for since it has numerous utility method. – user429035 Sep 03 '14 at 22:45
  • Could you add the output you are trying to achieve? –  Sep 03 '14 at 22:46
  • 7
    Right, but if there was such a concept, JS might have had support for getting the "parent object". Since it doesn't, you can't know whether a given object is a property value of another object or not. The only thing you can do is a) establish the connection "manually" when creating the objects (like I mentioned) or b) iterate over the properties of all potential "parent objects" and compare the property value against the given "child" object (recursively if need be). – Felix Kling Sep 03 '14 at 22:47
  • @FelixKling thanks! totally make sense . seems like option (a) is better than iterating over million of records. – user429035 Sep 03 '14 at 22:55
  • possible duplicate of [Javascript objects: get parent](http://stackoverflow.com/questions/2980763/javascript-objects-get-parent) – Evan Davis Sep 03 '14 at 23:03
  • @Mathletics nopes not a duplicate . Answered question already know what is the containing object whereas in this case this is a flatten array derived from different objects . – user429035 Sep 03 '14 at 23:20
  • If it's not possible when the object is known, it's doubly-impossible when the object is unknown. So it either _is_ a duplicate, or it's a question about comparing properties in objects (of which there are dozens of dupes.) – Evan Davis Sep 04 '14 at 16:07

1 Answers1

3

This is not directly possible, because as far as JavaScript knows, child1 could be a property of any number of objects: therefore, the concept of a 'parent object' does not really make sense generally speaking.

What's more, if there are no references to the parent objects, they will be garbage-collected, so no hope to get back to them.

However, if you can have a list of all the potential parents of an object, you can search them all :

function findParent(child){
  for(var i=0; i < parents.length; i++){
    var mainobject = parents[i]; // a potential parent
    for(childName in mainobject.prop3){
      if(mainobject.prop3[childName].id === child.id){ // match found!
        return mainobject;
      }
    }
  }
}
Valentin Waeselynck
  • 5,950
  • 26
  • 43