JSON return in an array in LOOP

2

I need to store the return of JSON to an array, that is:

                        $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "/engine/listarPublicacoes.php", 
                        success: function(data) {
                            for (var i=0;i<data.length;i++){
                                var availableTags = [
                                      {label:data[i+1], the_link:'http://intranet.supersoft.com.br/publicacao/'+data[i]+''}
                                ];
                                $( "#tags" ).autocomplete({
                                    source: availableTags,
                                    select:function(e,ui) { 
                                        window.location.href = ui.item.the_link;
                                    }
                                }); 
                            }   
                        }
                    }); 

The variable availableTags needs to save my information, but how can I put it in the loop for each info, have its object? my json goes like this:

["79","ADIANTAMENTO DE 13. SALARIO EM 26\/11\/14","78",null,"77",null,"76",null,"74",null,"73",null,"72","SS News NOV\/DEZ","71",null,"70","Facebook desenvolve rede social profissional ","69",null,"68","6 dicas para proteger dados no Whatsapp","61",null,"60",null,"59","Os ensinamentos de Martha Gabriel para o marketing na era digital","58","Pensamento do dia","57",null,"56","Gente que vira marca: o que aprender com o marketing das celebridades"]
    
asked by anonymous 28.11.2014 / 12:35

1 answer

4

Use the loop to mount the variable, and then start autocomplete:

var availableTags = [];

for (var i=0;i<data.length;i+=2){
    availableTags.push({label:data[i+1], the_link:'http://intranet.supersoft.com.br/publicacao/'+data[i]+''});
}  

$( "#tags" ).autocomplete({
    source: availableTags,
    select:function(e,ui) { 
        window.location.href = ui.item.the_link;
    }
}); 

The array is created before the loop, empty, and you use the push to add new objects.

    
28.11.2014 / 12:41