我有大量数据作为每个调用的响应,并以以下格式作为函数的参数进行检索。我需要将所有数据块附加到一个。
function getJSONdata(jsondata){
//Below is where I need help
//cumilatedJSONdata = cumilatedJSONdata + jsondata
}
以下是即将出现的对象的格式:
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
结构应类似
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"}}
]};
我尝试了类似的方法
var cumilatedJSONdata= {"results":[]}
function getJSONdata(jsondata){
var cumilatedJSONdata = $.extend(true,cumilatedJSONdata, jsondata
}
);
这不会保留以前的数据,而不会被新数据替换。
有人可以帮我吗?
答案 0 :(得分:1)
let results = [];
// do your async loop / promise loop here
while (needToGetResults) {
let newResponse = await asyncRequest();
results = results.concat(newResponse.results);
}
return results;
或者,
return {results:results}
答案 1 :(得分:1)
您可以尝试使用javascript的concat方法
cumilatedJSONdata.results = cumilatedJSONdata.results.concat(jsondata.results);