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.

5
  • var cumulatedData = ''; function getJSONdata(jsondata){ cumulatedData += jsondata; } Commented Jun 4, 2019 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. Commented Jun 4, 2019 at 11:04
  • Possible duplicate of stackoverflow.com/questions/5080028/… Commented Jun 4, 2019 at 11:06
  • Possible duplicate of stackoverflow.com/questions/1374126/… Commented Jun 4, 2019 at 11:08
  • I'm not exactly able to correlate those solutions here @www.admiraalit.nl Commented Jun 4, 2019 at 12:03

2 Answers 2

1

You can try using the concat method of javascript

cumilatedJSONdata.results = cumilatedJSONdata.results.concat(jsondata.results);
Sign up to request clarification or add additional context in comments.

4 Comments

This is giving as cumilatedJSONdata is not defined ReferenceError: cumilatedJSONdata is not defined
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);
Are you getting the jsondata from a request?
Thank you there was an issue with the scope and now its resolved
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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.