我正在尝试使用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"
                                 }]
                             }
                         ]
                    }

最佳答案

我想到的最快的解决方案。

以问题组为数组的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);

08-15 19:19