Grouping and summing return data json with jquery

0

I have this Json that returns from the database, I need to consolidate data that is the same but I have no idea how to do it in jquery or php. If anyone has any suggestions Thanks!

  {  
   "data":[  
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"1",
         "pergunta":"a",
         "qtd":"40"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"2",
         "pergunta":"b",
         "qtd":"29"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"2",
         "pergunta":"b",
         "qtd":"11"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"3"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"5"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"2"
      }
   ]
}
    
asked by anonymous 14.02.2018 / 17:52

2 answers

2

To reduce the array by leaving only one entry for each question, distinguishing through nr_pergunta , you can use the reduce .

For each element it looks for if it exists, and if there is already, sum the quantity. When it does not exist, it just adds it to the array being built.

Implementation:

let json = '
{
	"data": [{
			"nome_pesquisa": "teste",
			"nr_pergunta": "1",
			"pergunta": "a",
			"qtd": "40"
		},
		{
			"nome_pesquisa": "teste",
			"nr_pergunta": "2",
			"pergunta": "b",
			"qtd": "29"
		},
		{
			"nome_pesquisa": "teste",
			"nr_pergunta": "2",
			"pergunta": "b",
			"qtd": "11"
		},
		{
			"nome_pesquisa": "teste",
			"nr_pergunta": "3",
			"pergunta": "c",
			"qtd": "3"
		},
		{
			"nome_pesquisa": "teste",
			"nr_pergunta": "3",
			"pergunta": "c",
			"qtd": "5"
		},
		{
			"nome_pesquisa": "teste",
			"nr_pergunta": "3",
			"pergunta": "c",
			"qtd": "2"
		}
	]
}';

let perguntas = JSON.parse(json).data;
//tornar a quantidade numérica para a soma funcionar como esperado
perguntas.forEach(perg => perg.qtd = Number(perg.qtd)); 

let perguntasAgrupadas = perguntas.reduce(function(acc, curr){
  let encontrado = false;
  for (let pergunta of acc){
    if (pergunta.nr_pergunta == curr.nr_pergunta){
      pergunta.qtd += curr.qtd;
      encontrado = true;
      break;
    }
  }
  
  if (!encontrado)
    acc.push(curr);
  
  return acc;
}, []);

console.log(perguntasAgrupadas);
    
14.02.2018 / 19:21
0

You can also use filter after adding and deleting repeated items (remembering that the method below has IE compatibility):

var json = {  
   "data":[  
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"1",
         "pergunta":"a",
         "qtd":"40"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"2",
         "pergunta":"b",
         "qtd":"29"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"2",
         "pergunta":"b",
         "qtd":"11"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"3"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"5"
      },
      {  
         "nome_pesquisa":"teste",
         "nr_pergunta":"3",
         "pergunta":"c",
         "qtd":"2"
      }
   ]
}

var temp_array = [];

$.each(json.data, function(e,v){
   var items = v.nr_pergunta+v.pergunta;
   
   if(temp_array.indexOf(items) == -1){
      temp_array.push(e);
      temp_array.push(items);
   }else{
      var array_pos = temp_array[temp_array.indexOf(items)-1];
      var soma = parseInt(json.data[array_pos].qtd) + parseInt(v.qtd);
      json.data[array_pos].qtd = soma.toString();
      delete json.data[e];
   }
});

json = json.data.filter(function(n){ return n }); 

console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
14.02.2018 / 19:48