【问题标题】:Angular5: batch calls to get data for a filed in a json arrayAngular 5:批量调用以获取 json 数组中字段的数据
【发布时间】:2019-04-12 08:30:58
【问题描述】:

我有一个包含以下格式的 json 数据的数组

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

我正试图获得这些人的指定。有一个接受一组 id 的 API。我正在尝试分批获取 30 个对象的名称。即发送前 30 个对象并获取它们的名称并继续。我尝试保持 for 循环并传递 30 个对象但未成功。

Designation API 提供以下格式的数据。

[
  {
    "staffId": "26",
    "designation": "PRA"
  },
  {
    "staffId": "25",
    "designation": "MRA"
  }
]

结果 json

员工 = [ {“身份证”:1, “姓名”:“罗伯特”, "staffDesignation": "PRA" }, {“身份证”:2, “名称”:“彼得”, "staffDesignation": "MRA" } ]

因此,对于我获得的每 30 批指定,我需要使用该值更新员工记录。

staff.component.ts

for (让 i = 0; i { //这里传30个对象 //更新指示符逻辑 }, (错误) => {

})

}

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

}

【问题讨论】:

  • 那么staff 数组中的id 是否等于指定数组中的staffId?询问您在 OP 中给出的不同值。该 API 是否会按照对象在 staff 数组中出现的顺序返回数据?
  • @SiddAjmera 是的.. 我们从 API 调用中获取作为 staffId 的密钥.. 这与员工记录中的 id 相同。
  • @indra257 结果 josn 你怎么能在其中管理名字?并且您想根据结果 josn 更新键值。我说的对吗?
  • @SachinShah 是的
  • @indra257 所以基本上你想首先以 30 个批次调用所有 API,然后你想更新键和值。我说的对吗?

标签: angular angular5 angular6 lodash


【解决方案1】:

当您从 API 调用中获取 res 时,您可以开始在过滤器数组过程中工作。

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

在这种情况下,我建议您可以使用以下结构。

step1 : 制作一个包含 30 个批次的数组。

第 2 步: 使用带有 Observable 的 for 循环。

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

【讨论】:

  • 我认为我们不会以 30 个批次为单位拨打电话
  • @indra257 我已经更新了我的答案。请检查一下。 :)
  • 谢谢。我没有看到我有 30 个批次。我错过了什么吗?
  • 你必须从你身边设置批次。我们只是在想法上帮助您。
【解决方案2】:

将您的 staff 数据视为:

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

您的 API 会按照请求的顺序返回数据,

你可以得到staffIds:

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

调用 API 并获取 response 后,您可以将响应与 staff 数组合并,如下所示:

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

【讨论】:

  • 但是我需要分批拨打电话。比如通过前 30 个对象并获得它们的指定并重复该过程。我有近1000名员工,所以为了更好的表现,我必须分批打电话。
  • 我不认为我们在这里分批拨打 30 个电话
  • @indra257,我在答案中明确提到,一旦您获得数据,您就可以将响应与staff 数组合并。由于您尚未真正分享您是如何进行 API 调用的,因此我无法在答案中添加任何随机内容。
  • 感谢您的意见。请找到我迄今为止尝试过的更新问题。我还没有弄清楚如何分批传递 30 个对象。员工服务将循环遍历它以获取 id 并传递给 API
猜你喜欢
  • 2016-11-04
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
相关资源
最近更新 更多