dadidaochieng
dadidaochieng

Reputation: 105

Reconstruct angular java script array object

I need to reconstruct this array

var benefits = {
    "Emergency services": [
        "Emergency room care",
        "Ambulance service",
        "Urgent care centers/facilities"
    ],
    "Laboratory services": [
        "Lab tests & X-ray services",
        "Imaging/diagnostics (e.g.,  MRI, CT scan, PET scan"
    ],
};

To something like the one below before I display it our to the page with an ng-repeat angular directive

var planBenefit = {
    "Emergency services": [
        {
            benefit: "Emergency room care",
            category: "Emergency services",
            limit: "",
            limit_individual: "",
            max_day_limit: "",
            plan: "newPlan"
        },
        {
            benefit: "Ambulance service",
            category: "Emergency services",
            limit: "",
            limit_individual: "",
            max_day_limit: "",
            plan: "newPlan"
        },
        {
            benefit: "Urgent care centers/facilities",
            category: "Emergency services",
            limit: "",
            limit_individual: "",
            max_day_limit: "",
            plan: "newPlan"
        }
    ],
    "Laboratory services": [
        {
            benefit: "Lab tests & X-ray services",
            category: "Laboratory services",
            limit: "",
            limit_individual: "",
            max_day_limit: "",
            plan: "newPlan"
        },
        {
            benefit: "Imaging/diagnostics (e.g.,  MRI, CT scan, PET scan",
            category: "Laboratory services",
            limit: "",
            limit_individual: "",
            max_day_limit: "",
            plan: "newPlan"
        }
    ]
}

Anyone who can help with this. Am getting the data from an API but I need to display it on the page under the categories in the second array and capture more information before I send it back to a different API and save

Upvotes: 0

Views: 56

Answers (2)

Shaishab Roy
Shaishab Roy

Reputation: 16805

Can try it :

   var planBenefit = {};

   for (var key in benefits) {
       if (benefits.hasOwnProperty(key)) {

         planBenefit[key]= [];

         benefits[key].forEach(function(benefitName) {

           var newBenefit = {
                "benefit" : benefitName,
                "category" : key,
                "limit" : "",
                "limit_individual" : "",
                "max_day_limit" : "",
                "plan" : "newPlan"
           };

           planBenefit[key].push(newBenefit);

         });
       }
   }

Upvotes: 0

Shilly
Shilly

Reputation: 8589

You can use a map inside a reduction to quickly generate the structure you need. I'll leave getting everything into an angular directive to yourself, since I don't work with angular. Good luck.

   var planBenefit = Object.keys(benefits).reduce(function( map, key ) {
        map[key] = benefits[key].map(function( benefit ) {
            return {
                "benefit" : benefit,
                "category" : key,
                "limit" : "",
                "limit_individual" : "",
                "max_day_limit" : "",
                "plan" : "newPlan"
            };
        });
        return map;
    }, {});

Upvotes: 1

Related Questions