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

时间:2018-07-15 15:23:51

标签: javascript javascript-objects

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);    
}  
}}

使用npm请求,我正在获取json的正文并使用 JSON.parse(body)将下面的JSON文件转换为对象列表。我需要 访问子对象中的数组。但由于孩子反对 会根据我们提供的输入动态更改。我无法 在代码中获取子对象的数组。    任何人都可以帮忙从没有父对象的阵列中获取 访问子对象。

我需要删除动态更改的子对象并需要访问 父对象的数组。    注意:PARENT OBject不会根据input进行更改。它将基于输入内容具有或不具有值

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  "
        ]
    }
}

我已加粗**子对象,这些子对象将根据 输入。目前,我已经将Admit作为Postmantool中的输入

1 个答案:

答案 0 :(得分:0)

您可以使用Object.values检索所有子对象值而无需知道键。这将是所有子项的所有值的数组。因此,对于您的对象,您可以使用以下命令访问数组:

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])

您只需要小心一点,就知道可能有多少个孩子。的about假设只有一个,因此我们可以使用children[0]来访问它。