Group and add json return data with jquery

Asked

Viewed 501 times

0

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

  {  
   "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"
      }
   ]
}
  • Consolidate in what way? Add the values and leave only 1 of the equals?

  • that’s right, in case it would appear once each question with the sum total of it.

2 answers

2


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

For each element look for if it exists, and if it already exists add the amount. When it does not exist just add 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);

  • Isac Thank you so much! It worked Perfectly! I’ll read more about this "reduce function".

0

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

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>

  • dvd , thanks for the help, I will study the two options, both filter and reduce. Vlw!

Browser other questions tagged

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