JSON return in an array in the LOOP

Asked

Viewed 427 times

2

I need to save the return of JSON in an array, i.e.:

                        $.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 need to save my information, but how can I put it in the loop for each info, have your object? my json follows 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"]
  • 2

    After 48 questions, editors shouldn’t have to keep removing the noise from their posts. Please check the edits that are made in your questions not to keep repeating at all times the same mistakes. Thank you.

  • @brasofilo I’m sorry, is that sometimes I can not express myself right, I will pay more attention to the questions, thank you!

  • 2

    @Furlan you can vote on the answers that helped you too. Any answer (or even question) from website that somehow helped you or you think it would help other people. Especially it would be nice to vote on answers given to your question. The vote is as important or more important in certain circumstances than acceptance.

1 answer

4


Use the loop to mount the variable, and only then initialize the 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 empty loop, and you use the method push from her to add new objects.

  • thanks @bfavaretto! worked out!

Browser other questions tagged

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