Angular5: batch calls to get data for a filed in a json array

﹥>﹥吖頭↗ 提交于 2019-12-11 05:34:17

问题


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

}

回答1:


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... 
     })
}



回答2:


Considering your staff data as:

staff = [{
    "id": 1,
    "name": "Robert"
  },
  {
    "id": 2,
    "name": "Peter"
  }
]

And your API returns the data in the same order in which it was requested,

You can get the staffIds as:

staffIds = staff.map(item => item.id);

Once you call your API and get the response, you can then merge the response with the staff array like this:

resultingArray = Object.values(
  [...staff, ...response]
  .reduce((r, { staffId, id = staffId, ...rest }) => ({
    ...r,
    [id]: { id, ...r[id], ...rest }
  }), {})
);


来源:https://stackoverflow.com/questions/53214402/angular5-batch-calls-to-get-data-for-a-filed-in-a-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!