Reputation: 23
I've chunks of data coming as response of each call and is retrieved as parameter of a function in below format. I need to append all the chunks of data into one.
function getJSONdata(jsondata){
//Below is where I need help
//cumilatedJSONdata = cumilatedJSONdata + jsondata
}
Below is the Format of the objects coming:
var jsondata = {"results":[
{"code":"1101696","name":"1101696","price":{"formattedValue":"36.00
CAD"}},
{"code":"1101693","name":"1101693","price":{"formattedValue":"33.00
CAD"}},
{"code":"1101699","name":"1101699","price":{"formattedValue":"39.00
CAD"}}
]};
I want the cumilatedJSONdata
structure should be something like
var cumilatedJSONdata = {"results":[
{"code":"1101696","name":"1101696","price":{"formattedValue":"36.00
CAD"}},
{"code":"1101693","name":"1101693","price":{"formattedValue":"33.00
CAD"}},
{"code":"1101699","name":"1101699","price":{"formattedValue":"39.00
CAD"}},
{"code":"1101693","name":"1101693","price":{"formattedValue":"33.00
CAD"}},
{"code":"1101699","name":"1101699","price":{"formattedValue":"39.00
CAD"}},
{"code":"1101693","name":"1101693","price":{"formattedValue":"33.00
CAD"}},
{"code":"1101699","name":"1101699","price":{"formattedValue":"39.00
CAD"}}
]};
I tried something like this
var cumilatedJSONdata= {"results":[]}
function getJSONdata(jsondata){
var cumilatedJSONdata = $.extend(true,cumilatedJSONdata, jsondata
}
);
This is not persisting previous data and getting replaced with new data.
Could anyone please help me on this.
Upvotes: 0
Views: 82
Reputation: 68
You can try using the concat method of javascript
cumilatedJSONdata.results = cumilatedJSONdata.results.concat(jsondata.results);
Upvotes: 1
Reputation: 13892
let results = [];
// do your async loop / promise loop here
while (needToGetResults) {
let newResponse = await asyncRequest();
results = results.concat(newResponse.results);
}
return results;
alternatively,
return {results:results}
Upvotes: 1