Search code examples
javascriptjavascript-objects

Need to access the array of Child object from the Parent object in Javascript


PostResponseOutput(TextedValue):Promise<any>
{
var options = {
    method: 'POST',
    url: 'http://NHSSQLCHNE8105:8081/ctakes-web-rest/service/analyzejson? 
pipeline=default',
    headers: {
        'Accept': 'application/json',
        'Content-Type' : 'application/json'
    },
    body: TextedValue
};
await request(options, callback);
async function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        var info = await JSON.parse(body);    
}  
}}

Using the npm requests i am getting the body of the json and using the JSON.parse(body) Converts the below JSON file to list of objects. I need to access the array which is in the child object. But since the child object gets changed dynamically based on the input which we provide.I am unable to get the array of the child object in code. Can anyone please help in getting the array from the PARENT OBJECT WITHOUT ACCESSING THE Child object.

I need to remove the Dynamically changing child objects and need to access its array from the Parent Object. Note: PARENT OBject will not be changing based on the input . it will have the value or not based on the input

JSON File

{
// This is the Allergy Object. which doesn't have any Value for the input 
Admit
    "Allergy": {},
// This is the Others Object. which has 1 Value for the input Admit
    "Others": {
        **"Health Care Activity~T058~Hospital admission":** [
            "codingScheme: SNOMEDCT_US",
            "code: 32485007",
            "cui: C0184666",
            "semanticType: Health Care Activity",
            "tui: T058",
            "preferredtext: Hospital admission"
        ]
    },
// This is the AnatomicalSiteMention Object. which doesn't have any Value 
for the input Admit
    "AnatomicalSiteMention": {},
// This is the MedicationMention Object. which doesn't have any Value for the 
input Admit
    "MedicationMention": {},
// This is the DrugChangeStatusAnnotation Object. which doesn't have any 
Value for the input Admit
    "DrugChangeStatusAnnotation": {},
// This is the DrugChangeStatusAnnotation Object. which doesn't have any 
Value for the input Admit
    "StrengthAnnotation": {},
// This is the Request Object. which doesn't have any Value for the input 
Admit
    "Request": {},
// This is the FractionStrengthAnnotation Object. which doesn't have any 
Value for the input Admit
    "FractionStrengthAnnotation": {},
// This is the FrequencyUnitAnnotation Object. which doesn't have any Value 
for the input Admit
    "FrequencyUnitAnnotation": {},
// This is the DiseaseDisorderMention Object. which doesn't have any Value 
for the input Admit
    "DiseaseDisorderMention": {},
// This is the FamilyMember Object. which doesn't have any Value for the 
input Admit
    "FamilyMember": {},
// This is the SignSymptomMention Object. which doesn't have any Value for 
the input Admit
    "SignSymptomMention": {},
// This is the RouteAnnotation Object. which doesn't have any Value for the 
input Admit
    "RouteAnnotation": {},
// This is the DateAnnotation Object which doesn't have any Value for the 
input Admit
    "DateAnnotation": {},
// This is the MeasurementAnnotation Object. which doesn't have any Value for 
the input Admit
    "MeasurementAnnotation": {},
// This is the RelationDetails Object. which doesn't have 1 Child object 
Value  for the input Admit
    "RelationDetails": {
        **"RELATIONS:":** [
            "\n"
        ]
    },
// This is the TimeMention. which doesn't have 1 Child object Value  for the 
input Admit
    "TimeMention": {},
// This is the ProcedureMention. which doesn't have 1 Child object Value  for 
the input Admit
    "ProcedureMention": {},
// This is the StrengthUnitAnnotation object. which doesn't have 1 Child 
object Value  for the input Admit
    "StrengthUnitAnnotation": {},
// This is the Health Care activity Object. which doesn't have 1 Child object 
Value  for the input Admit
    "Health Care activity": {
        **"Hospital admission":** [
            "start:1",
            "end:3",
            "polarity:1",
            "[codingScheme: SNOMEDCT_US, code: 32485007, cui: C0184666, 
semanticType: Health Care Activity, tui: T058, preferredtext: Hospital 
admission]"
        ]
    },
// This is the AnalysisText object which doesn't have 1 Child object Value  
for the input Admit
    "AnalysisText": {
        **"AnalysisText":** [
            "admit\n  ",
            "admit\n  "
        ]
    }
}

I have bolded** the child objects which will change dynamically based on the input. Currently i have given Admit as the input in Postmantool


Solution

  • You can retrieve all the child objects values without knowing the keys using Object.values. This will be an array of all the values of all the children. So in the case of your object you can access the array with:

    let Admit = {
        "Allergy": {},
        "Others": {
            "Health Care Activity~T058~Hospital admission": [
                    "codingScheme: SNOMEDCT_US",
                    "code: 32485007",
                    "cui: C0184666",
                    "semanticType: Health Care Activity",
                    "tui: T058",
                    "preferredtext: Hospital admission"
                ]
        },
         "AnatomicalSiteMention": {}
         // etc...
    }
    
    let othersObj = Admit.Others
    // children will have all the values of `others` children
    // you don't need to know 'Health Care Activity~T058~Hospital admission' 
    let children = Object.values(othersObj)
    console.log(children[0])

    You just need to be careful that you know how many children there might be. The about assumed that there was only one so we could access it with children[0].