Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Symfony : Add data from Ajax call to js array

I have an Ajax request that fetch data in database.

These data may vary in function of the action that calls the ajax request.

Each time the Ajax request is called, I want some ot these datas to be pushed in a javascript array declared outside of the Ajax function.

The problem is, each time Ajax is called, my function manage to put the wanted data in the array but erases the previous data.

Here is the script :

<script>

let myArray = [];

function fetchWeeksForViewportWidth(startDate) {

    //AJAX
    $.ajax({
        method: "GET",
            url: "{{path('days_json')}}",
            data: {
                //some data
            },
            success: function (data) {

                let trParents = document.getElementsByClassName('project-assignment');
               
                $.each(trParents, function(key, parent) {
                    let assId = parent.dataset.assignmentId;

                    myArray[assId] = [];

                    $.each(data['days'], function(key, value) {
                        
                        myArray[assId][value.code] = 0;
                        
                        //some other treatment
                        
                        if(value.code in data['game']) {
                        
                            myArray[assId][value.code] = data['game'][value.code];
                        
                        });
                    });
                });
            },
            error: function() {
                //handle error
                console.log("error")
            }
    });
}

$(document).ready( function () {


    function displayArray(){
        console.log(myArray);

        setTimeout(displayArray, 5000);
    }

    displayArray();

});

</script>

Any idea why new data don't add to the ones from previous ajax calls but keep replacing them ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of assignment

myArray[assId][value.code] = 0;

try the push() function of the array

myArray[assId].push(0);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda