Grouping into dynamic objects

Asked

Viewed 33 times

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 times when I will have more questions than this example. So I need to group the same questions into objects because each object will feed a graph 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 idea how to do that? I appreciate the help!

1 answer

1


In your question you did not identify the return of Php as an array with the presence of '[]' and the separation of items with ',' so consider your PHP returning an array of the objects cited in the question, for example:

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

and 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]);
	}

  • Jonas, my friend, worked perfectly, very thankful!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.