skip to Main Content

I am having Array of object like below

[
  {
    "HLT": "Infections NEC",
    "LLT": "Adrenalitis",
    "HLGT": "Infections - pathogen unspecified",
    "PT": "Adrenalitis",
    "SOC": "Infections and infestations"
  },
  {
    "HLT": "Adrenal neoplasms",
    "LLT": "Adrenal cyst",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal cyst",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal mass",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal mass",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenomegaly",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenomegaly",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal nodule",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal mass",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland therapeutic procedures",
    "LLT": "Adrenalectomy",
    "HLGT": "Endocrine gland therapeutic procedures",
    "PT": "Adrenalectomy",
    "SOC": "Surgical and medical procedures"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal crisis",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenocortical insufficiency acute",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal atrophy",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal atrophy",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Endocrine neoplasms malignant and unspecified NEC",
    "LLT": "Adrenal neoplasm",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal neoplasm",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal disorders congenital",
    "LLT": "Adrenal virilism",
    "HLGT": "Endocrine disorders congenital",
    "PT": "Adrenogenital syndrome",
    "SOC": "Congenital, familial and genetic disorders"
  },
  {
    "HLT": "Adrenal neoplasms benign",
    "LLT": "Adrenal adenoma",
    "HLGT": "Endocrine neoplasms benign",
    "PT": "Adrenal adenoma",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal hematoma",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal haematoma",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal bleeding",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal haemorrhage",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal disorder",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal disorder",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Cerebral disorders congenital",
    "LLT": "Adrenoleukodystrophy",
    "HLGT": "Neurological disorders congenital",
    "PT": "Adrenoleukodystrophy",
    "SOC": "Congenital, familial and genetic disorders"
  },
  {
    "HLT": "Abdominal and gastrointestinal injuries NEC",
    "LLT": "Adrenal gland injury",
    "HLGT": "Injuries NEC",
    "PT": "Adrenal gland injury",
    "SOC": "Injury, poisoning and procedural complications"
  },
  {
    "HLT": "Metastases to specified sites",
    "LLT": "Adrenal metastases",
    "HLGT": "Metastases",
    "PT": "Metastases to adrenals",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal neoplasms malignant",
    "LLT": "Adrenal gland cancer",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal gland cancer",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal neoplasms benign",
    "LLT": "Adrenal myelolipoma",
    "HLGT": "Endocrine neoplasms benign",
    "PT": "Benign neoplasm of adrenal gland",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Autonomic nervous system disorders",
    "LLT": "Adrenolytic syndrome",
    "HLGT": "Neuromuscular disorders",
    "PT": "Adrenolytic syndrome",
    "SOC": "Nervous system disorders"
  },
  {
    "HLT": "Endocrine analyses and imaging NEC",
    "LLT": "Adrenal scintigraphy",
    "HLGT": "Endocrine investigations (incl sex hormones)",
    "PT": "Scan adrenal gland",
    "SOC": "Investigations"
  },
  {
    "HLT": "Pituitary analyses anterior",
    "LLT": "Adrenocorticotropin",
    "HLGT": "Endocrine investigations (incl sex hormones)",
    "PT": "Blood corticotrophin",
    "SOC": "Investigations"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal thrombosis",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal thrombosis",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal hypofunction",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal insufficiency",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland therapeutic procedures",
    "LLT": "Adrenalectomy NOS",
    "HLGT": "Endocrine gland therapeutic procedures",
    "PT": "Adrenalectomy",
    "SOC": "Surgical and medical procedures"
  },
  {
    "HLT": "Endocrine neoplasms malignant and unspecified NEC",
    "LLT": "Adrenal neoplasm NOS",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal neoplasm",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal neoplasms malignant",
    "LLT": "Adrenal carcinoma",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal gland cancer",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Autonomic nervous system disorders",
    "LLT": "Adrenergic syndrome",
    "HLGT": "Neuromuscular disorders",
    "PT": "Adrenergic syndrome",
    "SOC": "Nervous system disorders"
  },
  {
    "HLT": "Adrenal gland histopathology procedures",
    "LLT": "Adrenal gland biopsy",
    "HLGT": "Endocrine investigations (incl sex hormones)",
    "PT": "Biopsy adrenal gland",
    "SOC": "Investigations"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal haematoma",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal haematoma",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal haemorrhage",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal haemorrhage",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal hemorrhage",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal haemorrhage",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal disorder NOS",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal disorder",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal suppression",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal suppression",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal disorders congenital",
    "LLT": "Adrenogenital syndrome",
    "HLGT": "Endocrine disorders congenital",
    "PT": "Adrenogenital syndrome",
    "SOC": "Congenital, familial and genetic disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal cortex necrosis",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal cortex necrosis",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal cortex atrophy",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal atrophy",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Hormonal therapeutic procedures NEC",
    "LLT": "Adrenal blockade therapy",
    "HLGT": "Endocrine gland therapeutic procedures",
    "PT": "Adrenal blockade therapy",
    "SOC": "Surgical and medical procedures"
  },
  {
    "HLT": "Adrenal gland therapeutic procedures",
    "LLT": "Adrenal gland operation",
    "HLGT": "Endocrine gland therapeutic procedures",
    "PT": "Adrenal gland operation",
    "SOC": "Surgical and medical procedures"
  },
  {
    "HLT": "Endocrine neoplasms malignant and unspecified NEC",
    "LLT": "Adrenal incidentaloma",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal neoplasm",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal neoplasms malignant",
    "LLT": "Adrenal adenocarcinoma",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenal gland cancer",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Adrenal neoplasms malignant",
    "LLT": "Adrenocortical carcinoma",
    "HLGT": "Endocrine neoplasms malignant and unspecified",
    "PT": "Adrenocortical carcinoma",
    "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
  },
  {
    "HLT": "Infections NEC",
    "LLT": "Adrenal gland abscess",
    "HLGT": "Infections - pathogen unspecified",
    "PT": "Adrenal gland abscess",
    "SOC": "Infections and infestations"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal gland enlarged",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenomegaly",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal cortical necrosis",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal cortex necrosis",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal gland disorders NEC",
    "LLT": "Adrenal cortex dysplasia",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal cortex dysplasia",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal insufficiency",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal insufficiency",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hypofunctions",
    "LLT": "Adrenal insufficiency NOS",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal insufficiency",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hyperfunctions",
    "LLT": "Adrenal androgen excess",
    "HLGT": "Adrenal gland disorders",
    "PT": "Adrenal androgen excess",
    "SOC": "Endocrine disorders"
  },
  {
    "HLT": "Adrenal cortical hyperfunctions",
    "LLT": "Adrenal hypercorticism",
    "HLGT": "Adrenal gland disorders",
    "PT": "Hyperadrenocorticism",
    "SOC": "Endocrine disorders"
  }
]

And I want the data like it in below way as a tree

Expectation is like above image. also we can may use below prefix like
const prefix = [SOC,HLGT,HLT,PT,LLT];

We can also create data json structure like – parent and children
Like –

[
  {
    key: 'SOC',
    value: 'Infections and infestations',
    children: [
      {
        key: 'HLGT',
        value: 'value one',
        children: [
          
        ]
      }
    ]
  }
]

key value with children pairs as a tree. for exact result please refer the screen shot attached

I have tried it with many things but not bale to achive above image type of result.

2

Answers


  1. You could group by the given keys and return an object with the wanted grouing for every level.

    const
        data = [{ HLT: "Infections NEC", LLT: "Adrenalitis", HLGT: "Infections - pathogen unspecified", PT: "Adrenalitis", SOC: "Infections and infestations" }, { HLT: "Adrenal neoplasms", LLT: "Adrenal cyst", HLGT: "Adrenal gland disorders", PT: "Adrenal cyst", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal mass", HLGT: "Adrenal gland disorders", PT: "Adrenal mass", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenomegaly", HLGT: "Adrenal gland disorders", PT: "Adrenomegaly", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal nodule", HLGT: "Adrenal gland disorders", PT: "Adrenal mass", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland therapeutic procedures", LLT: "Adrenalectomy", HLGT: "Endocrine gland therapeutic procedures", PT: "Adrenalectomy", SOC: "Surgical and medical procedures" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal crisis", HLGT: "Adrenal gland disorders", PT: "Adrenocortical insufficiency acute", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal atrophy", HLGT: "Adrenal gland disorders", PT: "Adrenal atrophy", SOC: "Endocrine disorders" }, { HLT: "Endocrine neoplasms malignant and unspecified NEC", LLT: "Adrenal neoplasm", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal neoplasm", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal disorders congenital", LLT: "Adrenal virilism", HLGT: "Endocrine disorders congenital", PT: "Adrenogenital syndrome", SOC: "Congenital, familial and genetic disorders" }, { HLT: "Adrenal neoplasms benign", LLT: "Adrenal adenoma", HLGT: "Endocrine neoplasms benign", PT: "Adrenal adenoma", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal hematoma", HLGT: "Adrenal gland disorders", PT: "Adrenal haematoma", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal bleeding", HLGT: "Adrenal gland disorders", PT: "Adrenal haemorrhage", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal disorder", HLGT: "Adrenal gland disorders", PT: "Adrenal disorder", SOC: "Endocrine disorders" }, { HLT: "Cerebral disorders congenital", LLT: "Adrenoleukodystrophy", HLGT: "Neurological disorders congenital", PT: "Adrenoleukodystrophy", SOC: "Congenital, familial and genetic disorders" }, { HLT: "Abdominal and gastrointestinal injuries NEC", LLT: "Adrenal gland injury", HLGT: "Injuries NEC", PT: "Adrenal gland injury", SOC: "Injury, poisoning and procedural complications" }, { HLT: "Metastases to specified sites", LLT: "Adrenal metastases", HLGT: "Metastases", PT: "Metastases to adrenals", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal neoplasms malignant", LLT: "Adrenal gland cancer", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal gland cancer", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal neoplasms benign", LLT: "Adrenal myelolipoma", HLGT: "Endocrine neoplasms benign", PT: "Benign neoplasm of adrenal gland", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Autonomic nervous system disorders", LLT: "Adrenolytic syndrome", HLGT: "Neuromuscular disorders", PT: "Adrenolytic syndrome", SOC: "Nervous system disorders" }, { HLT: "Endocrine analyses and imaging NEC", LLT: "Adrenal scintigraphy", HLGT: "Endocrine investigations (incl sex hormones)", PT: "Scan adrenal gland", SOC: "Investigations" }, { HLT: "Pituitary analyses anterior", LLT: "Adrenocorticotropin", HLGT: "Endocrine investigations (incl sex hormones)", PT: "Blood corticotrophin", SOC: "Investigations" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal thrombosis", HLGT: "Adrenal gland disorders", PT: "Adrenal thrombosis", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal hypofunction", HLGT: "Adrenal gland disorders", PT: "Adrenal insufficiency", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland therapeutic procedures", LLT: "Adrenalectomy NOS", HLGT: "Endocrine gland therapeutic procedures", PT: "Adrenalectomy", SOC: "Surgical and medical procedures" }, { HLT: "Endocrine neoplasms malignant and unspecified NEC", LLT: "Adrenal neoplasm NOS", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal neoplasm", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal neoplasms malignant", LLT: "Adrenal carcinoma", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal gland cancer", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Autonomic nervous system disorders", LLT: "Adrenergic syndrome", HLGT: "Neuromuscular disorders", PT: "Adrenergic syndrome", SOC: "Nervous system disorders" }, { HLT: "Adrenal gland histopathology procedures", LLT: "Adrenal gland biopsy", HLGT: "Endocrine investigations (incl sex hormones)", PT: "Biopsy adrenal gland", SOC: "Investigations" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal haematoma", HLGT: "Adrenal gland disorders", PT: "Adrenal haematoma", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal haemorrhage", HLGT: "Adrenal gland disorders", PT: "Adrenal haemorrhage", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal hemorrhage", HLGT: "Adrenal gland disorders", PT: "Adrenal haemorrhage", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal disorder NOS", HLGT: "Adrenal gland disorders", PT: "Adrenal disorder", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal suppression", HLGT: "Adrenal gland disorders", PT: "Adrenal suppression", SOC: "Endocrine disorders" }, { HLT: "Adrenal disorders congenital", LLT: "Adrenogenital syndrome", HLGT: "Endocrine disorders congenital", PT: "Adrenogenital syndrome", SOC: "Congenital, familial and genetic disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal cortex necrosis", HLGT: "Adrenal gland disorders", PT: "Adrenal cortex necrosis", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal cortex atrophy", HLGT: "Adrenal gland disorders", PT: "Adrenal atrophy", SOC: "Endocrine disorders" }, { HLT: "Hormonal therapeutic procedures NEC", LLT: "Adrenal blockade therapy", HLGT: "Endocrine gland therapeutic procedures", PT: "Adrenal blockade therapy", SOC: "Surgical and medical procedures" }, { HLT: "Adrenal gland therapeutic procedures", LLT: "Adrenal gland operation", HLGT: "Endocrine gland therapeutic procedures", PT: "Adrenal gland operation", SOC: "Surgical and medical procedures" }, { HLT: "Endocrine neoplasms malignant and unspecified NEC", LLT: "Adrenal incidentaloma", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal neoplasm", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal neoplasms malignant", LLT: "Adrenal adenocarcinoma", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenal gland cancer", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Adrenal neoplasms malignant", LLT: "Adrenocortical carcinoma", HLGT: "Endocrine neoplasms malignant and unspecified", PT: "Adrenocortical carcinoma", SOC: "Neoplasms benign, malignant and unspecified (incl cysts and polyps)" }, { HLT: "Infections NEC", LLT: "Adrenal gland abscess", HLGT: "Infections - pathogen unspecified", PT: "Adrenal gland abscess", SOC: "Infections and infestations" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal gland enlarged", HLGT: "Adrenal gland disorders", PT: "Adrenomegaly", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal cortical necrosis", HLGT: "Adrenal gland disorders", PT: "Adrenal cortex necrosis", SOC: "Endocrine disorders" }, { HLT: "Adrenal gland disorders NEC", LLT: "Adrenal cortex dysplasia", HLGT: "Adrenal gland disorders", PT: "Adrenal cortex dysplasia", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal insufficiency", HLGT: "Adrenal gland disorders", PT: "Adrenal insufficiency", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hypofunctions", LLT: "Adrenal insufficiency NOS", HLGT: "Adrenal gland disorders", PT: "Adrenal insufficiency", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hyperfunctions", LLT: "Adrenal androgen excess", HLGT: "Adrenal gland disorders", PT: "Adrenal androgen excess", SOC: "Endocrine disorders" }, { HLT: "Adrenal cortical hyperfunctions", LLT: "Adrenal hypercorticism", HLGT: "Adrenal gland disorders", PT: "Hyperadrenocorticism", SOC: "Endocrine disorders" }],
        keys = ['SOC', 'HLGT', 'HLT', 'PT', 'LLT'],
        groupBy = (data, keys) => {
            if (!keys.length) return [];
    
            const
                key = keys[0],
                getValue = key => object => object[key];
    
            return Object
                .entries(Object.groupBy(data, getValue(key)))
                .map(([value, data]) => ({ key, value, children: groupBy(data, keys.slice(1)) }));
        },
        result = groupBy(data, keys);
    
    console.log(result); 
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. Check it.

    const data = [
      {
        "HLT": "Infections NEC",
        "LLT": "Adrenalitis",
        "HLGT": "Infections - pathogen unspecified",
        "PT": "Adrenalitis",
        "SOC": "Infections and infestations"
      },
      {
        "HLT": "Adrenal neoplasms",
        "LLT": "Adrenal cyst",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal cyst",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal mass",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal mass",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenomegaly",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenomegaly",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal nodule",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal mass",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland therapeutic procedures",
        "LLT": "Adrenalectomy",
        "HLGT": "Endocrine gland therapeutic procedures",
        "PT": "Adrenalectomy",
        "SOC": "Surgical and medical procedures"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal crisis",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenocortical insufficiency acute",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal atrophy",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal atrophy",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Endocrine neoplasms malignant and unspecified NEC",
        "LLT": "Adrenal neoplasm",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal neoplasm",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal disorders congenital",
        "LLT": "Adrenal virilism",
        "HLGT": "Endocrine disorders congenital",
        "PT": "Adrenogenital syndrome",
        "SOC": "Congenital, familial and genetic disorders"
      },
      {
        "HLT": "Adrenal neoplasms benign",
        "LLT": "Adrenal adenoma",
        "HLGT": "Endocrine neoplasms benign",
        "PT": "Adrenal adenoma",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal hematoma",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal haematoma",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal bleeding",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal haemorrhage",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal disorder",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal disorder",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Cerebral disorders congenital",
        "LLT": "Adrenoleukodystrophy",
        "HLGT": "Neurological disorders congenital",
        "PT": "Adrenoleukodystrophy",
        "SOC": "Congenital, familial and genetic disorders"
      },
      {
        "HLT": "Abdominal and gastrointestinal injuries NEC",
        "LLT": "Adrenal gland injury",
        "HLGT": "Injuries NEC",
        "PT": "Adrenal gland injury",
        "SOC": "Injury, poisoning and procedural complications"
      },
      {
        "HLT": "Metastases to specified sites",
        "LLT": "Adrenal metastases",
        "HLGT": "Metastases",
        "PT": "Metastases to adrenals",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal neoplasms malignant",
        "LLT": "Adrenal gland cancer",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal gland cancer",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal neoplasms benign",
        "LLT": "Adrenal myelolipoma",
        "HLGT": "Endocrine neoplasms benign",
        "PT": "Benign neoplasm of adrenal gland",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Autonomic nervous system disorders",
        "LLT": "Adrenolytic syndrome",
        "HLGT": "Neuromuscular disorders",
        "PT": "Adrenolytic syndrome",
        "SOC": "Nervous system disorders"
      },
      {
        "HLT": "Endocrine analyses and imaging NEC",
        "LLT": "Adrenal scintigraphy",
        "HLGT": "Endocrine investigations (incl sex hormones)",
        "PT": "Scan adrenal gland",
        "SOC": "Investigations"
      },
      {
        "HLT": "Pituitary analyses anterior",
        "LLT": "Adrenocorticotropin",
        "HLGT": "Endocrine investigations (incl sex hormones)",
        "PT": "Blood corticotrophin",
        "SOC": "Investigations"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal thrombosis",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal thrombosis",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal hypofunction",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal insufficiency",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland therapeutic procedures",
        "LLT": "Adrenalectomy NOS",
        "HLGT": "Endocrine gland therapeutic procedures",
        "PT": "Adrenalectomy",
        "SOC": "Surgical and medical procedures"
      },
      {
        "HLT": "Endocrine neoplasms malignant and unspecified NEC",
        "LLT": "Adrenal neoplasm NOS",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal neoplasm",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal neoplasms malignant",
        "LLT": "Adrenal carcinoma",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal gland cancer",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Autonomic nervous system disorders",
        "LLT": "Adrenergic syndrome",
        "HLGT": "Neuromuscular disorders",
        "PT": "Adrenergic syndrome",
        "SOC": "Nervous system disorders"
      },
      {
        "HLT": "Adrenal gland histopathology procedures",
        "LLT": "Adrenal gland biopsy",
        "HLGT": "Endocrine investigations (incl sex hormones)",
        "PT": "Biopsy adrenal gland",
        "SOC": "Investigations"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal haematoma",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal haematoma",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal haemorrhage",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal haemorrhage",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal hemorrhage",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal haemorrhage",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal disorder NOS",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal disorder",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal suppression",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal suppression",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal disorders congenital",
        "LLT": "Adrenogenital syndrome",
        "HLGT": "Endocrine disorders congenital",
        "PT": "Adrenogenital syndrome",
        "SOC": "Congenital, familial and genetic disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal cortex necrosis",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal cortex necrosis",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal cortex atrophy",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal atrophy",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Hormonal therapeutic procedures NEC",
        "LLT": "Adrenal blockade therapy",
        "HLGT": "Endocrine gland therapeutic procedures",
        "PT": "Adrenal blockade therapy",
        "SOC": "Surgical and medical procedures"
      },
      {
        "HLT": "Adrenal gland therapeutic procedures",
        "LLT": "Adrenal gland operation",
        "HLGT": "Endocrine gland therapeutic procedures",
        "PT": "Adrenal gland operation",
        "SOC": "Surgical and medical procedures"
      },
      {
        "HLT": "Endocrine neoplasms malignant and unspecified NEC",
        "LLT": "Adrenal incidentaloma",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal neoplasm",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal neoplasms malignant",
        "LLT": "Adrenal adenocarcinoma",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenal gland cancer",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Adrenal neoplasms malignant",
        "LLT": "Adrenocortical carcinoma",
        "HLGT": "Endocrine neoplasms malignant and unspecified",
        "PT": "Adrenocortical carcinoma",
        "SOC": "Neoplasms benign, malignant and unspecified (incl cysts and polyps)"
      },
      {
        "HLT": "Infections NEC",
        "LLT": "Adrenal gland abscess",
        "HLGT": "Infections - pathogen unspecified",
        "PT": "Adrenal gland abscess",
        "SOC": "Infections and infestations"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal gland enlarged",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenomegaly",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal cortical necrosis",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal cortex necrosis",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal gland disorders NEC",
        "LLT": "Adrenal cortex dysplasia",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal cortex dysplasia",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal insufficiency",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal insufficiency",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hypofunctions",
        "LLT": "Adrenal insufficiency NOS",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal insufficiency",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hyperfunctions",
        "LLT": "Adrenal androgen excess",
        "HLGT": "Adrenal gland disorders",
        "PT": "Adrenal androgen excess",
        "SOC": "Endocrine disorders"
      },
      {
        "HLT": "Adrenal cortical hyperfunctions",
        "LLT": "Adrenal hypercorticism",
        "HLGT": "Adrenal gland disorders",
        "PT": "Hyperadrenocorticism",
        "SOC": "Endocrine disorders"
      }
    ]
    
    
        const treeData = {
          name: "My Tree",
          children: []
        };
    
        data.forEach((item) => {
          let currentNode = treeData;
          ["SOC", "HLGT", "HLT", "PT", "LLT"].forEach((key) => {
            let childNode = currentNode.children.find((child) => child.name === item[key]);
            if (!childNode) {
              childNode = { name: item[key], children: [] };
              currentNode.children.push(childNode);
            }
            currentNode = childNode;
          });
        });
    
        const width = 1960;
        const height = 1600;
    
        const svg = d3.select("svg")
          .attr("width", width)
          .attr("height", height)
          .append("g")
          .attr("transform", "translate(40,0)");
    
        const tree = d3.tree().size([height, width - 160]);
    
        const root = d3.hierarchy(treeData);
        tree(root);
    
        const link = svg.selectAll(".link")
          .data(root.links())
          .enter().append("path")
          .attr("class", "link")
          .attr("d", d3.linkHorizontal()
            .x((d) => d.y)
            .y((d) => d.x));
    
        const node = svg.selectAll(".node")
          .data(root.descendants())
          .enter().append("g")
          .attr("class", "node")
          .attr("transform", (d) => `translate(${d.y},${d.x})`);
    
        node.append("circle")
          .attr("r", 4.5);
    
        node.append("text")
          .attr("dy", ".31em")
          .attr("x", (d) => d.children ? -13 : 13)
          .style("text-anchor", (d) => d.children ? "end" : "start")
          .text((d) => d.data.name);
    .node circle {
          fill: #fff;
          stroke: steelblue;
          stroke-width: 1.5px;
        }
    
        .node text {
          font: 12px sans-serif;
        }
    
        .link {
          fill: none;
          stroke: #ccc;
          stroke-width: 1.5px;
        }
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <svg width="960" height="600"></svg>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search