简体   繁体   English

需要从Javascript中的Parent对象访问Child对象的数组

[英]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. 使用npm请求,我得到json的主体,并使用JSON.parse(body)将下面的JSON文件转换为对象列表。 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. 任何人都可以在不访问Child对象的情况下帮助从父对象获取数组。

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 . 注意:PARENT OBject不会根据input进行更改。 it will have the value or not based on the input 根据输入是否具有值

JSON File JSON文件

{
// 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 目前,我已经将Admit作为Postmantool中的输入

You can retrieve all the child objects values without knowing the keys using Object.values . 您可以使用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] . 的about假设只有一个,因此我们可以使用children[0]访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM