对象的动态分组

时间:2018-05-30 14:34:50

标签: jquery

这是我的回报。

        {"question":"Name","answer":"jorge","qt":"1","graph":"table"}
        {"pergunta":"Name","answer":"Jorge","qt":"2","graph":"table"}
        {"question":"Name","answer":"Jhon","qt":"1","graph":"table"}
        {"question":"what is your favorite color ?","answer":"red","qt":"1","graph":"column"}
        {"question":"what is your favorite color ?","answer":"blue","qt":"1","graph":"column"}
        {"question":"what is your favorite color ?","answer":"yellow","qt":"1","graph":"column"}
        {"question":"Are you over 20 years old?","answer":"No","qt":"1","graph":"pie"}
        {"question":"Are you over 20 years old?","answer":"Yes","qt":"3","graph":"pie"}

我要做的是将每组问题分组到一个对象中,以便将此对象用作图形,但我不知道如何完成此操作

    {data : [{"question":"Are you over 20 years old?","answer":"No","qt":"1","graph":"pie"},
    {"question":"Are you over 20 years old?","answer":"Yes","qt":"3","graph":"pie"}]}

    {data :[{"question":"what is your favorite color ?","answer":"red","qt":"1","graph":"column"},
    {"question":"what is your favorite color ?","answer":"blue","qt":"1","graph":"column"},
    {"question":"what is your favorite color ?","answer":"yellow","qt":"1","graph":"column"}]}

    {data :[{"question":"Name","answer":"jorge","qt":"1","graph":"table"},
    {"question":"Name","answer":"Jorge","qt":"2","graph":"table"},
    {"question":"Name","answer":"Jhon","qt":"1","graph":"table"}]}

感谢您的帮助

正在使用此代码但未使用

var array_original = [];
var array_none  = [];
var array_table = [];
var array_col   = [];
var array_bar   = [];  
var array_pie   = [];  
$.each(data.data,function(k,v){
    var dataPergunta = v.pergunta;
    var dataGrafico  = v.grafico;
    array_original.push({
        "pergunta" : dataPergunta,
        "resposta" : v.resposta,
        "grafico"  : dataGrafico, 
        "qt"       : v.qt
    });
var novaArray;
   if(dataPergunta == v.pergunta && dataGrafico == "Column"){
      novaArray = array_col;
   }else if(dataPergunta == v.pergunta && dataGrafico == "Bar"){
      novaArray = array_bar;
   }else if(dataPergunta == v.pergunta && dataGrafico == "Table"){
      novaArray = array_table;
   }else if (dataPergunta == v.pergunta && dataGrafico == "Pie"){
      novaArray = array_pie;
   }else if (dataPergunta == v.pergunta && dataGrafico == "none"){
      novaArray = array_none;
   }
 if(novaArray){
      novaArray.push({
         "pergunta" : dataPergunta,
         "resposta" : v.resposta,
         "grafico"  : dataGrafico,
         "qt"       : v.qt
      });
   }
});

0 个答案:

没有答案