Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda