如何将所有页面合并为一个结果

huangapple go评论63阅读模式
英文:

How to get all pages into a single result

问题

I have a following API call

this.httpCall.getAllItems().subscribe(results => {
  for (const each of results) {
    .....
  }
 ....
});

getAllItems() 是一个基本的HTTP API调用,默认返回10条记录。总数在标头 X-Total-Count 中,例如为65

getAllItems(): Observable<any> {
  const url = `URL/items?count=true`;
  return this.http.get(url, { observe: 'response' }));
}

总数在 results.headers.get('X-Total-Count'); 中获取。

要获取下一页,需要在API中发送偏移量。因此,要获取所有65条记录,可以这样做

URL/items?limit=10&offset=0
URL/items?limit=10&offset=10
URL/items?limit=10&offset=20
URL/items?limit=10&offset=30
URL/items?limit=10&offset=40
URL/items?limit=10&offset=50
URL/items?limit=10&offset=60

URL/items?limit=10&offset=70 

时将返回0个。

如何在 this.httpCall.getAllItems().subscribe(results => 中实现上述逻辑,以便将所有结果合并在一起,然后进行for循环?

我了解到switchMap可能对此有所帮助,但我需要一些关于如何在上述逻辑中实现它的帮助。

英文:

I have a following API call

this.httpCall.getAllItems().subscribe(results => {
  for (const each of results) {
    .....
  }
 ....
});

The getAllItems() is a basic HTTP API call which returns a result of 10 records by default. The total count comes in header X-Total-Count which for example is 65

getAllItems(): Observable<any> {
  const url = `URL/items?count=true`;
  return this.http.get(url, { observe: 'response' }));
}

The total count comes in results.headers.get('X-Total-Count');.

To get the next page I need to send offset in API. So, to get all 65 records, I would do

URL/items?limit=10&offset=0
URL/items?limit=10&offset=10
URL/items?limit=10&offset=20
URL/items?limit=10&offset=30
URL/items?limit=10&offset=40
URL/items?limit=10&offset=50
URL/items?limit=10&offset=60

At

URL/items?limit=10&offset=70 

There will be 0 return.

How do I implement this above in this.httpCall.getAllItems().subscribe(results => so that I can join all the results in one and then do the for loop continuation?

I read that perhaps switchMap can help here, but I need some help on how to implement it in above logic.

答案1

得分: 1

首先,您需要调用带有偏移量参数的函数以获取项目。

getItems(offset: number): Observable<any> {
  const url = `URL/items`; 
  return this.http.get(url, { 'limit': 10, 'offset': offset }));
}

您还需要调用以获取总记录数的函数。

getNumberOfItems(): Observable<any> {
  const url = `URL/items?count=true`;
  return this.http.get(url, { observe: 'response' }));
}

然后,订阅它并在循环中使用偏移量作为参数调用getItems函数。

this.httpCall.getNumberOfItems().subscribe(result: any => {
    let allItems = []
    const totalCount = result.headers.get('X-Total-Count');
    const iterations = Math.ceil(totalCount/10) 
    for (let i = 0; i < iterations; i++) {
        this.httpCall.getItems(i*10).subscribe(listOfItems: any => {
           allItems.concat(listOfItems) 
        })
    }
});

这里查找ceil的用法
以及这里查找concat的用法

英文:

First of all you will need a call to get items with offset as a parameter.

getItems(offset : number): Observable&lt;any&gt; {
  const url = `URL/items`; 
  return this.http.get(url, { &#39;limit&#39;: 10, &#39;offset&#39;: offset }));
}

you will also need a call to get the number of total records

getNumberOfItems(): Observable&lt;any&gt; {
  const url = `URL/items?count=true`;
  return this.http.get(url, { observe: &#39;response&#39; }));
}

then subscribe to it and call the getItems function with offset as parameter in a for loop

this.httpCall.getNumberOfItems().subscribe(result: any =&gt; {
    let allItems = []
    const totalCount = result.headers.get(&#39;X-Total-Count&#39;);
    const iterations = Math.ceil(totalCount/10) 
    for (let i = 0; i &lt; iterations; i++) {
        this.httpCall.getItems(i*10).subscribe(listOfItems: any =&gt; {
           allItems.concat(listOfItems) 
        })
    }
});

Find here what ceil does
and here what concat does

huangapple
  • 本文由 发表于 2023年4月13日 14:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定