I have no idea to write a general function for this problem, for example Reflection in C #, but this function can help, although it is not general.
var Obj = {
a: {
name: 'X',
age: 'Y',
other: {
job: 'P',
location: {
lat: 31.88,
lng: 71.88
}
}
}
};
function getParentObject(key,obj){
switch(key){
case "lat" || "lng": return obj.a.other.location; break;
default:return obj;
}
}
var lng = getParentObject("lat",Obj).lng;
source
share