0

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

1
  • This problem actually has nothing to do with multidimensional arrays (nor AngularJs actually), please remove those tags :) Commented Apr 15, 2016 at 12:15

2 Answers 2

1

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;
    }, {});
Sign up to request clarification or add additional context in comments.

1 Comment

nice implementation!
0

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

         });
       }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.