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

185
Views
Why is jQuery returning undefined array?

I'm trying to read a json file from the server using jQuery in order to use that data to create specific functions on the site. The json file is an array of objects.

function populate() {
  var dataArr = [];
  $.getJSON('http://localhost/teamproject/item_data.json', function(data) {
    for (let i = 0; i < data.length; i++) {
      dataArr.push([data[i].name,
                    data[i].price,
                    data[i].postage,
                    data[i].itemID,
                    data[i].start,
                    data[i].end]);
                    
    }
    return dataArr;
  });
}

And then

function filterTime(change) {
  var selectedValue = change.value;
  let data = populate();

  console.log(data);
}

The console.log(data); prints undefined. How can I return this array correctly in the populate() function?

about 4 years ago · Juan Pablo Isaza
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!