Return Json and Put in Multidimensional Array

0

I'm making a request to assemble a chart using Flot Js.

I'm returning a Json like this:

[{
    "status": "Finalizado",
    "id_status": "4",
    "total_chamado": "2",
    "mes": "4",
    "ano": "2015"
}, {
    "status": "Finalizado",
    "id_status": "4",
    "total_chamado": "1",
    "mes": "5",
    "ano": "2015"
}, {
    "status": "Finalizado",
    "id_status": "4",
    "total_chamado": "1",
    "mes": "6",
    "ano": "2015"
}, {
    "status": "Finalizado",
    "id_status": "4",
    "total_chamado": "1",
    "mes": "7",
    "ano": "2015"
}, {
    "status": "Em Aberto",
    "id_status": "1",
    "total_chamado": "1",
    "mes": "8",
    "ano": "2015"
}, {
    "status": "Finalizado",
    "id_status": "4",
    "total_chamado": "1",
    "mes": "8",
    "ano": "2015"
}, {
    "status": "Em Aberto",
    "id_status": "1",
    "total_chamado": "3",
    "mes": "9",
    "ano": "2015"
}, {
    "status": "Em Aberto",
    "id_status": "1",
    "total_chamado": "1",
    "mes": "10",
    "ano": "2015"
}, {
    "status": "Em Aberto",
    "id_status": "1",
    "total_chamado": "1",
    "mes": "11",
    "ano": "2015"
}]

And I need it in JavaScript to look like this:

vetor = [ [mes, total], [mes, total], [mes, total], [mes, total], [mes, total], [mes, total] ];

I'm trying like this, but it's not working. The graph does not mount.

var caixa_entrada = []
$.ajax({
   type: "GET",
   cache: false,
   url: baseURL + '/dashboard/grafico-chamado',
   dataType: 'json',
   success: function(data){
      $.each(data, function(index, value){
         caixa_entrada.push([index, value.total_chamado]);
      });
   }
});
    
asked by anonymous 18.11.2015 / 20:08

1 answer

0

Finally, the problem that was happening to me was unrelated to the setting of% multidimensional% for the assembly of the graph.

The problem was in the variable array , which was the caixa_entrada of the function. This variable is used in the function that mounts the data and values for the chart, but was getting value callback .

Then, I started to think about what it might be and soon realized that this variable was undefined and wanted to use it out of scope. That's no way to keep you, little Padawan!

To solve it I did so:

var caixa_entrada = []
$.ajax({
   type: "GET",
   cache: false,
   url: baseURL + '/dashboard/grafico-chamado',
   dataType: 'json',
   async: false,
   success: function(data){
      $.each(data, function(index, value){
         caixa_entrada.push([index, value.total_chamado]);
      });
   }
});

I added the callback parameter to make a synchronous and asynchronous request , where this function is executed after the graph.

When it is synchronous it runs before from the function that mounts the graph so I can use the variable.

I do not like theories, I like code examples. So I must have explained more or less, more or less, which means async: false . Of course, that should not even be your job.

So if anyone explains it better, it would be nice, although I've seen some questions related here in SOpt.

    
19.11.2015 / 12:52