Reputation: 288
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"
}
]
}
]
}
]
}
]
}
Upvotes: 1
Views: 2433
Reputation: 14487
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?)
Upvotes: 1
Reputation: 4175
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
Upvotes: 0
Reputation: 275
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.
Upvotes: 1