重建角度java脚本数组对象

时间:2016-04-15 11:59:14

标签: javascript arrays json

我需要重建这个数组

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"
    ],
};

在我使用ng-repeat angular指令

显示页面之前,请使用下面的内容
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"
        }
    ]
}

任何可以提供帮助的人。我从API获取数据但我需要在第二个数组中的类别下的页面上显示它并在我将其发送回不同的API并保存之前捕获更多信息

2 个答案:

答案 0 :(得分:1)

您可以使用缩小内的地图快速生成所需的结构。我会把所有的东西都留给你自己的角度指示,因为我不使用角度。祝你好运。

   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;
    }, {});

答案 1 :(得分:0)

可以尝试一下:

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

         });
       }
   }