简体   繁体   中英

how can create a deeply nested object in javascript?

I want to create nested object in deeply way. What I'm trying to create is a grouped summary object from the array of nested JSON objects with the following structure:

[
    {
        "id": 1,
        "code": "193",
        "type": "Dental Provider",
        "classification": "Dentis",
        "specialization": "General Practice",
        "definition": "definition1",
        "notes": "[7/1/2003: new]",
        "individual": "individual"},
    {
        "id": 11,
        "code": "207",
        "type": "Allopathic & Osteopathic Physicians",
        "classification": "Anesthesiology",
        "specialization": "Pediatric Anesthesiology",
        "definition": "defin_2",
        "individual": "individual"}]

Expected:

result = {
    "invidual": {
        "Dental Provider": {
            "Dentis":{
                "General Practice":{
                    id: 1,
                    code: "193",
                    definition: "definition1",
                    individual: "individual",
                    type: "Dental Provider",
                    classification: "Dentist",
                    specialization: "General Practice"},
"invidual": {
        "Allopathic & Osteopathic Physicians": {
            "Anesthesiology":{
                "Pediatric Anesthesiology":{
                    id: 11,
                    code: "207",
                    definition: "defin_2",
                    individual: "individual",
                    type: "Allopathic & Osteopathic Physicians",
                    classification: "Anesthesiology",
                    specialization: "Pediatric Anesthesiology"}
}
let result = taxonomies.reduce((c, v) => {
  c[v.individual] = c[v.individual] || {};                         
  c[v.individual][v.type] = c[v.individual][v.type] || {};   
  c[v.individual][v.type][v.classification] = c[v.individual][v.type][v.classification] || {};    
  c[v.individual][v.type][v.classification][v.specialization] = v
console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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