Grouping in dynamic objects

0

My php is returning this data:

 {"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"}

This would be the return in jquery that I need to do dynamically because there will be moments that I will have more questions than this example. So I need to group the same questions into objects because each object would feed a graphic into canvas js.

{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"}]}

Any ideas how to do this? Thanks for the help!

    
asked by anonymous 30.05.2018 / 19:24

1 answer

1

In your question did not identify the return of Php as an array with the presence of '[]' and separation of items with ',' then Consider your PHP return an array of objects quoted in the question, eg: p>

var retornoPHP = [{"question":"Name","answer":"jorge","qt":"1","graph":"table"},
     {"question":"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"}];

Then do:

var retornoPhp = [{"question":"Name","answer":"jorge","qt":"1","graph":"table"},
         {"question":"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"}];

	var arrayAgrupado = retornoPhp.reduce(function(ant, atu){ 
		if(ant.length > 0 && ant[ant.length - 1].data.length > 0 && ant[ant.length - 1].data[0].question == atu.question) { 
			ant[ant.length - 1].data.push(atu);
			return ant;
		} else {
			return ant.concat([{data: [atu]}]);
		}
	}, []);

	for (var i = 0; i < arrayAgrupado.length; i++) {
		//cada loop desse for é um objeto com as perguntas agrupadas
		console.log(arrayAgrupado[i]);
	}
    
31.05.2018 / 05:17