任务-我必须将响应数组合并为一个。我正在调用相同的API,但传递了不同的“跳过”参数。跳过参数是动态的,基于最后的响应。
Call #1 - api(id, skip=0)
Call #2 - api(id, skip+1)
...
这是我的代码:
function appendArray(jobId, skip, count) {
appSegmentService.csvDownload(jobId, skip, count)
.then(function(resp) {
var arr = [];
arr = arr.concat(resp.data);
skip += 5000;
if (resp.data.length === 5000) {
arr = arr.concat(appendArray(jobId, skip, count));
} else {
console.log(arr);
}
}, function(err) {
console.log(err);
});
}
答案 0 :(得分:0)
我建议您使用lodash包,因为它对所有数组操作的访问都非常方便https://lodash.com/docs/
var _ = require('lodash');
var arr=[];
function appendArray(jobId, skip, count) {
var localArray=appSegmentService.csvDownload(jobId, skip, count)
.then(function (resp) {
var arr = [];
arr = arr.concat(resp.data);
skip += 5000;
if (resp.data.length === 5000) {
return arr = arr.concat(appendArray(jobId, skip, count));
} else {
console.log(arr);
}
}, function (err) {
console.log(err);
});
arr=_.concat(arr,localArray);
return arr;
}