Angular5:批处理调用以获取json数组中文件的数据 [英] Angular5: batch calls to get data for a filed in a json array
问题描述
我有一个具有以下格式的json数据的数组
I have an array with json data in the below format
staff = [
{ "id" : 1,
"name" : "Robert"
},
{ "id" : 2,
"name" : "Peter"
}
]
我正试图得到这些人的任命.有一个API可以接受一组ID.我正尝试以30为批次获得指定.即,先发送30个对象并获得其指定并继续..我尝试保持for循环并传递30个对象,但未成功.
I am trying to get the designation of these people. There is an API which accepts group of ids. I am trying to get designations in batches of 30. i.e send first 30 objects and get their designations and go on.. I tried keeping a for loop and pass 30 objects but unsuccessful.
Designation API以以下格式提供数据.
Designation API gives data in the below format.
[
{
"staffId": "26",
"designation": "PRA"
},
{
"staffId": "25",
"designation": "MRA"
}
]
结果json
工作人员= [ {"id":1, 名称":罗伯特", "staffDesignation":"PRA" }, {"id":2, "name":"Peter", "staffDesignation":"MRA" } ]
staff = [ { "id" : 1, "name" : "Robert", "staffDesignation": "PRA" }, { "id" : 2, "name" : "Peter", "staffDesignation": "MRA" } ]
因此,在这里,我每获得30个批次的指定,就需要用该值更新人员记录.
So here for every 30 batches of designations that I get, I need to update the staff record with that value.
staff.component.ts
staff.component.ts
for(让i = 0; i< = this.staff.length; i ++){ this.staffService.getStaffDesignator(//应该传递30个对象).subscribe((designator)=> {//这里传递30个对象 //更新指定符逻辑 }, (err)=> {
for (let i = 0; i <= this.staff.length; i++) { this.staffService.getStaffDesignator(//should pass 30 objects).subscribe((designator) => { //Here pass 30 objects //update designator logic }, (err) => {
})
}
staff.service.ts
staff.service.ts
getStaffDesignator(staff)
{
staff.forEach((staff, index) => {
if (index === 0) {
url = url + `?contactId=${staff.id}`;
}
else {
url = url + `&contactId=${staff.id}`
}
}) //loop through the objects to get the staff id to pass to the APIcall
return this.http.get(url, this.options)
.map((res: Response) => {
return res.json();
}) //API call to get designations for staff
}
推荐答案
从API调用获取资源后,您就可以开始在过滤器阵列过程中工作了.
While you get res from API call then you can start to work in filter array process.
var filterArray = [];
this.http.post(url , param , header , function(err , response) => {
// First way is to use map
// You can use for loop
for(let i = 0 i < response.length ; i++){
var obj = {
id: response[i].staffId,
name: response[i].name,
staffDesignation: response[i].designation,
}
this.filterArray.push(obj);
// Now you can call next API for another batch...
}
})
在这种情况下,建议您使用以下结构.
In this case , I suggest you can use this below structure.
第1步::排列30个批次的阵列.
step1 : Make a array of 30 batches.
第2步::用于Observable
的循环.
for(let i = 0 ; i < this.array.length ; i++){ //Each i has batch of 30.
// Hear observable will work as a promise in for loop to make call in sync.
return new Observable((observer) => {
return http.post(url , param , option)...
//Once you got data from API use my above code to filter your
// Key and value from res.
observable.next();
observable.complete(); // It will go for call next batch...
})
}
这篇关于Angular5:批处理调用以获取json数组中文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!