0

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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
LNP
  • 23
  • 4
  • `var cumulatedData = ''; function getJSONdata(jsondata){ cumulatedData += jsondata; }` – GrafiCode Jun 04 '19 at 11:04
  • 1
    You are dealing with regular JavaScript objects, not JSON. The JSON format is for *serialised* data and it's text. Since you have actual objects and arrays, you can simply combine the arrays. – VLAZ Jun 04 '19 at 11:04
  • Possible duplicate of https://stackoverflow.com/questions/5080028/what-is-the-most-efficient-way-to-concatenate-n-arrays – www.admiraalit.nl Jun 04 '19 at 11:06
  • Possible duplicate of https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating – www.admiraalit.nl Jun 04 '19 at 11:08
  • I'm not exactly able to correlate those solutions here @www.admiraalit.nl – LNP Jun 04 '19 at 12:03

2 Answers2

1
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}
TKoL
  • 13,158
  • 3
  • 39
  • 73
1

You can try using the concat method of javascript

cumilatedJSONdata.results = cumilatedJSONdata.results.concat(jsondata.results);
Naga Raj
  • 68
  • 7
  • This is giving as cumilatedJSONdata is not defined ReferenceError: cumilatedJSONdata is not defined – LNP Jun 04 '19 at 11:16
  • Try running this: var cumilatedJSONdata = {"results":[]}; 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"}} ]}; cumilatedJSONdata.results = cumilatedJSONdata.results.concat(jsondata.results); – Naga Raj Jun 04 '19 at 11:22
  • Are you getting the jsondata from a request? – Naga Raj Jun 04 '19 at 11:39
  • Thank you there was an issue with the scope and now its resolved – LNP Jun 04 '19 at 12:19