1

Is it possible to get the parent object from a child object dynamically? Essentially, all I'm trying to accomplish is to dynamically retrieve the value of a property belonging to a child objects' parent. For example, in the following Json, I want to extract the driver of a particular car.:

   {
        "driver": [
            {
                "id": 1,       |
                "name": "Bob", |=> this is the parent
                "age": "34",   |
                "car": [
                    {
                        "make": "BMW",     |
                        "model": "3.20",   | this is the child
                        "colour": "Silver",|
                        "mileage": [
                            {
                                "total": "350523",
                                "year": [
                                    {
                                        "2011": "3535",
                                        "2012": "7852",
                                        "2013": "8045"
                                    }
                                ],
                                "month": [
                                    {
                                        "december": "966",
                                        "november": "546",
                                        "october": "7657"
                                    }
                                ]
                            }
                        ]
                    }
                ]
         }
        ]
    }
6
  • which is parent which is child. what you are exactly looking for?? Commented Jul 3, 2017 at 16:33
  • Can you check the question again ?? I clarified it Commented Jul 3, 2017 at 16:42
  • how are you going to get the parent component driver information. Where you are using it? Commented Jul 3, 2017 at 16:44
  • You want to find the parent using the child object? Commented Jul 3, 2017 at 16:51
  • Yes that's it, I want to get the parent using the child object Commented Jul 3, 2017 at 16:53

3 Answers 3

1

Using for loops:

for(let parent of data.driver) {
   for(let car of parent.car) {
      if(car.make === 'BMW') {
          // can do what you like with 'parent'
      }
   }
}

Using filter() or find() (standard javascript):

drivers_who_drive_bwm = data.driver.filter((parent) => {
    // find() will give -1, if no car was found that matched 
    //    car.make === 'BWM'
    return parent['car'].find((car) => car.make === 'BWM') !== -1
})

Also:

Your naming conventions are confusing. I would expect driver.car to be a single car, in your code it's array of cars. If it always contains single car, then it would be better not to use array. Same for .driver. Better key would be .drivers to indicate multiple drivers. (but maybe it is XML converted to json, in that case you are stuck with it?)

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was looking for, Thanks
1

Whatever strategy you choose, you will basically be iterating through and returning. So I feel the "best" strategy is using what you are most comfortable with.

Typescript is just Javascript. So if you are comfortable with a Javascript-ey "functional programming" way of doing things you can use Array map & filter.

You of course will have to deal with application specific logic you have not specified like "What happens when the same make/model exists across different drivers?".

If you are not comfortable with functional programming you can always build up a series of maps and then perform lookups.

But if you need to get it right, always do what you are comfortable doing.

Comments

0

To answer this question, an Object reference is just a memory location. there is no concept of parents comes here. its may not have any parent (just a logical thinking as parent, so may not any other object have property having reference to it), or may a lot of object have referred to same memory location (i.e. multiple parent by your logic).

1> So Either you can put parent reference to each child element programitically. Note, here you cannot do by parsing a JSON string, because its contains only JSON data, not reference as parse-able.

2> Or else Try to find out the driver object (i.e. parent object) having child object which contains your value according to your condition. you ca use filter , map of array functions in javascript to do so. but whatever you are doing is just iterating and find. in that case underscrore js will be a good library to use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.