由AngularJS将json格式转换为另一种json格式

时间:2016-08-04 16:17:04

标签: javascript arrays angularjs json

我正在尝试使用angularjs实现调查问卷,所以我有以下响应数组,我需要将此对象数组转换为json格式,如下面的输出,那么如何将对象数组转换为json格式?

 var response=[{"questiongroup":1,"question":1,"response":"response1"},
                     {"questiongroup":1,"question":2,"response":"response2"},
                     {"questiongroup":1,"question":3,"response":"response3"},
                     {"questiongroup":1,"question":4,"response":"response4"},
                     {"questiongroup":1,"question":5,"response":"response5"},
                     {"questiongroup":2,"question":6,"response":"response6"},
                     {"questiongroup":2,"question":7,"response":"response7"},
                     {"questiongroup":2,"question":8,"response":"response8"},
                     {"questiongroup":2,"question":9,"response":"response9"},
                     {"questiongroup":2,"question":10,"response":"response10"}];

期望的输出

   {"questiongroups": [
                       {
                         "questiongroup": 1,
                         "question": [
                           {
                             "question": 1,
                             "response":"response1"
                           },
                           {
                             "question": 2,
                             "response":"response2"
                            },
                            {
                             "question": 3,
                             "response":"response3"
                             },
                            {
                             "question": 4,
                             "response":"response4"
                              },
                             {
                              "question": 5,
                              "response":"response5"
                             }]
                         },
                         {
                             "questiongroup": 2,
                             "question": [
                               {
                                 "question": 6,
                                 "response":"response6"
                               },
                               {
                                 "question": 7,
                                 "response":"response7"
                                },
                                {
                                 "question": 8,
                                 "response":"response8"
                                 },
                                {
                                 "question": 9,
                                 "response":"response9"
                                  },
                                 {
                                  "question": 10,
                                  "response":"response10"
                                 }]
                             }
                         ]
                    }

2 个答案:

答案 0 :(得分:1)

最快的解决方案。

使用问题组作为数组的ID减少对对象的响应。然后,要根据需要返回,映射键以生成数组并使用questiongroups值创建对象。



var response=[{"questiongroup":1,"question":1,"response":"response1"},
                     {"questiongroup":1,"question":2,"response":"response2"},
                     {"questiongroup":1,"question":3,"response":"response3"},
                     {"questiongroup":1,"question":4,"response":"response4"},
                     {"questiongroup":1,"question":5,"response":"response5"},
                     {"questiongroup":2,"question":6,"response":"response6"},
                     {"questiongroup":2,"question":7,"response":"response7"},
                     {"questiongroup":2,"question":8,"response":"response8"},
                     {"questiongroup":2,"question":9,"response":"response9"},
                     {"questiongroup":2,"question":10,"response":"response10"}];

var questiongroups = response.reduce(function(groups, question){
  var group = groups[question.questiongroup] || [];
  group.push({
    question: question.question,
    response: question.response
  });

  groups[question.questiongroup] = group;
  
  return groups;
}, {});

questiongroups = Object.keys(questiongroups).map(function (key) {return {questiongroup: key, question: questiongroups[key]}});

var object = {
  questiongroups: questiongroups
};

console.log(object);




答案 1 :(得分:0)



var response=[{"questiongroup":1,"question":1,"response":"response1"},
                     {"questiongroup":1,"question":2,"response":"response2"},
                     {"questiongroup":1,"question":3,"response":"response3"},
                     {"questiongroup":1,"question":4,"response":"response4"},
                     {"questiongroup":1,"question":5,"response":"response5"},
                     {"questiongroup":2,"question":6,"response":"response6"},
                     {"questiongroup":2,"question":7,"response":"response7"},
                     {"questiongroup":2,"question":8,"response":"response8"},
                     {"questiongroup":2,"question":9,"response":"response9"},
                     {"questiongroup":2,"question":10,"response":"response10"}];

var result = {
	"questiongroups": [],
}

var group = {
	"questiongroup": 1,
  "question": []
}

for (var i in response) {

	if (response[i].questiongroup != group.questiongroup) {
  	result.questiongroups.push(group);
    group = {
    "questiongroup" : response[i].questiongroup,
  	"question": []
    };
  }
  
  var entry = {
    "question" : {
      "question" : response[i].question,
      "response" : response[i].response
    }
  }
  
  group.question.push(entry);
}

result.questiongroups.push(group);

console.log(result);