Return Json and Put to Multidimensional Array

Asked

Viewed 711 times

0

I’m making a request to plot 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 that in Javascript he stays that way:

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 doesn’t 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]);
      });
   }
});
  • I did a test here (apart from the ajax part) and the loop seems to work, assembling a multidimensional array. Is there no problem in some other part of the code?

  • Oh my... I know what it is! But I don’t remember how to solve it. The variable is inside a callback. Like I’m taking her out ?

  • yeah, could be scope problem even.

  • Solved. I put async: false in options.

  • Glad you solved it. I’ll delete my comments here.

1 answer

0


Finally, the problem that was happening to me had no relation to the assembly of the array multidimensional for graphic assembly.

The problem was in the variable caixa_entrada, that was the callback function. This variable is used in the function that mounts the data and values to the graph, but was getting value undefined.

And then I started thinking about what it could be and I soon realized that this variable was a callback and wanted to use outside the scope. So can’t keep you, little Padawan!

To solve 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 parameter async: false to make a request synchronous and not asynchronous, in which this function is performed afterward of the function that mounts the graph.

When she’s synchronous is executed before of the function that mounts the graph and so I can use the variable.

I don’t like theories, I like examples of code. So I must have explained more or less, more for less, which means the async:false. And of course, that’s probably not all you’re supposed to do.

So if someone explains it better, it would be nice, although I’ve seen some related questions here at Sopt.

Browser other questions tagged

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