Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!