I have a function, let's call it GetData(). I get the data from a query in another function and run an ajax call to populate the data. Once this operation is complete, I send the data to another function, let's call it PlaceData(). Each ajax call to GetData() puts the data in an object. I then send each object to PlaceData() and I want to collect these objects into an array in PlaceData() via the push() method, but each time it just adds a new array instead of sending the current one The objects are collected together, so I only get individual objects, not a collection. How do I get them collected into an array?
So here is the code sample I'm using:
function GetData(query) { var json = ""; var api_url = ('https://jsondata.site?conn={conn}&query={query}'); $.ajax({async: false; url: api_url, method: "GET"}).done(function(data){ json = JSON.parse(data); }); PlaceData(json); }; function PlaceData(data) { var objCollect = []; objCollect.push(data); console.log(objCollect); };
I want objCollect[] to retain all objects passed in, but actually I just get a new array containing each individual object.
P粉3511384622023-09-16 10:39:06
You need to use global scope variables to store objects. Define objCollect
outside the function and it should now hold all values.
var objCollect = []; function GetData(query) { var json = ""; var api_url = ('https://jsondata.site?conn={conn}&query={query}'); $.ajax({async: false; url: api_url, method: "GET"}).done(function(data){ json = JSON.parse(data); }); PlaceData(json); }; function PlaceData(data) { objCollect.push(data); console.log(objCollect); };