Angular5:批量调用以获取 json 数组中某个文件的数据 [英] Angular5: batch calls to get data for a filed in a json array
问题描述
我有一个包含以下格式的 json 数据的数组
员工 = [{id":1,姓名":罗伯特"},{id":2,姓名":彼得"}]
我正在尝试获取这些人的名称.有一个 API 接受一组 id.我正在尝试分批获取 30 个指定.即发送前 30 个对象并获取它们的指定并继续......我尝试保持 for 循环并传递 30 个对象但未成功.
指定 API 以以下格式提供数据.
<预><代码>[{"员工编号": "26",名称":PRA"},{"员工编号": "25",名称":MRA"}]结果 json
员工 = [{id":1,"name" : "罗伯特","员工名称": "PRA"},{id":2,"name" : "彼得","staffDesignation": "MRA"}]
因此,对于我获得的每 30 批指定,我需要使用该值更新员工记录.
staff.component.ts
for (let i = 0; i <= this.staff.length; i++) {this.staffService.getStaffDesignator(//应该传递30个对象).subscribe((designator) => {//这里传递30个对象//更新指示符逻辑},(错误)=> {
})
}
staff.service.ts
getStaffDesignator(staff){工作人员.forEach((工作人员,索引)=> {如果(索引=== 0){url = url + `?contactId=${staff.id}`;}别的 {url = url + `&contactId=${staff.id}`}})//遍历对象以获取要传递给APIcall的员工ID返回 this.http.get(url, this.options).map((res: 响应) => {返回 res.json();})//API调用以获取员工的名称}
当你从 API 调用中获得 res 时,你就可以开始在过滤器数组过程中工作了.
var filterArray = [];this.http.post(url , param , header , function(err , response) => {//第一种方式是使用地图//你可以使用for循环for(let i = 0 i < response.length ; i++){变量对象 = {id: response[i].staffId,名称:响应[i].name,工作人员指定:响应[i].指定,}this.filterArray.push(obj);//现在您可以为另一批次调用下一个 API...}})
在这种情况下,我建议您可以使用以下结构.
step1 : 制作 30 个批次的数组.
step2 : 将 for 循环与 Observable
一起使用.
for(let i = 0 ; i {返回 http.post(url , param , option)...//一旦你从API获得数据,使用我上面的代码来过滤你的//来自 res.key 的 key 和 valueobservable.next();observable.complete();//它将去调用下一批...})}
I have an array with json data in the below format
staff = [
{ "id" : 1,
"name" : "Robert"
},
{ "id" : 2,
"name" : "Peter"
}
]
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 gives data in the below format.
[
{
"staffId": "26",
"designation": "PRA"
},
{
"staffId": "25",
"designation": "MRA"
}
]
Result json
staff = [ { "id" : 1, "name" : "Robert", "staffDesignation": "PRA" }, { "id" : 2, "name" : "Peter", "staffDesignation": "MRA" } ]
So here for every 30 batches of designations that I get, I need to update the staff record with that value.
staff.component.ts
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
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
}
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.
step1 : Make a array of 30 batches.
step2 : Use for loop with 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屋!