How do I destructure this deep nested json objects and map it in JS

Viewed 49

I have a nested array like below. There are about 100 de objects in the array. The de objects also have deg[0] array but most likely I will only have the first index. Now the trick is that the de are subset of deg. Which means each deg can have say 10 de. How can I retrieve the deg and there associated de and map it into a new array like:

newArray = [
deg1: [
{de1},
{de2}
],
deg2: [
{de1},
{de2}
]
]

Here is my nested array. I posted four but the list is over a 100.

{
    "name": "Report",
    "id": "2YYUEZ6I1r9",
    "dse1": [
        {
            "de1": {
                "name": "Number",
                "id": "HjMOngg3kuy",
                "de1-av": [
                    {
                        "value": "FHaQMPv9zc7",
                        "attribute": {
                            "id": "uwVkIP7PZDt"
                        }
                    },
                    {
                        "value": "something",
                        "attribute": {
                            "id": "FHaQMPv9zc7"
                        }
                    }
                ],
                "deg1": [
                    {
                        "name": "TB",
                        "id": "2XJB1JO9qX8"
                    }
                ]
            }
        },
        {
            "de2": {
                "name": "Number of",
                "id": "a3dtGETTawy",
                "de2-av": [
                    {
                        "value": "FHaQMPv9zc7",
                        "attribute": {
                            "id": "uwVkIP7PZDt"
                        }
                    },
                    {
                        "value": "something",
                        "attribute": {
                            "id": "FHaQMPv9zc7"
                        }
                    }
                ],
                "deg1": [
                    {
                        "name": "Secondary",
                        "id": "w99RWzXHgtw"
                    }
                ]
            }
        },
        {
            "de1": {
                "name": "Number of",
                "id": "a3dtGETTawy",
                "de1av": [
                    {
                        "value": "FHaQMPv9zc7",
                        "attribute": {
                            "id": "uwVkIP7PZDt"
                        }
                    },
                    {
                        "value": "something",
                        "attribute": {
                            "id": "FHaQMPv9zc7"
                        }
                    }
                ],
                "deg2": [
                    {
                        "name": "Secondary",
                        "id": "w99RWzXHgtw"
                    }
                ]
            }
        },
        {
            "de2": {
                "name": "Number of",
                "id": "a3dtGETTawy",
                "de2av": [
                    {
                        "value": "FHaQMPv9zc7",
                        "attribute": {
                            "id": "uwVkIP7PZDt"
                        }
                    },
                    {
                        "value": "something",
                        "attribute": {
                            "id": "FHaQMPv9zc7"
                        }
                    }
                ],
                "deg2": [
                    {
                        "name": "Tertiary",
                        "id": "w99RWzXHgtw"
                    }
                ]
            }
        }
    ]
}
1 Answers

Group array of objects by property (this time a property to be matched by a reg exp) using Array.reduce.

Update: Ignoring missing keys.

var input={name:"Report",id:"2YYUEZ6I1r9",dse1:[{de1:{name:"Number",id:"HjMOngg3kuy","de1-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"TB",id:"2XJB1JO9qX8"}]}},{de2:{name:"Number of",id:"a3dtGETTawy","de2-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de1:{name:"Number of",id:"a3dtGETTawy",de1av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de2:{name:"Number of",id:"a3dtGETTawy",de2av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Tertiary",id:"w99RWzXHgtw"}]}}]}

var reg = new RegExp("^de[0-9]+$");
var reg2 = new RegExp("^deg[0-9]+$");

let obj = input['dse1'].reduce(function(agg, item) {

  // do your group by logic below this line

  var key = Object.keys(item).find(function(key) {
    return key.match(reg) ? key : null;
  })
  
  if (key) {
    var key2 = Object.keys(item[key]).find(function(key) {
      return key.match(reg2) ? key : null;
    })

    agg[key] = agg[key] || [];

    if (key2) {
      var to_push = {}
      to_push[key2] = item[key][key2]
      agg[key].push(to_push)
    }
    
  }
  // do your group by logic above this line

  return agg
}, {});

console.log(obj)
.as-console-wrapper {
  max-height: 100% !important;
}

Related