How to receive and collect a passed object from another function in JavaScript?
P粉413704245
P粉413704245 2023-09-15 13:12:20
0
1
555

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粉413704245
P粉413704245

reply all (1)
P粉351138462

You need to use global scope variables to store objects. DefineobjCollectoutside 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); };
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!