如何在 map 之前在 rxjs 中保存值

how to save value in rxjs before map

我的 odata 结果看起来像这样

我想从这里和之前获取 "value" 我想将 @odata.count 保存在 localStorage 中或者可能只是记录它。

  getAllBankAccounts(url: string): Observable<BankAccount[]> {
return this.http.get(url).pipe(map((res) => res['value']));
}

我如何 return 观察数组 "value" 属性 但在此之前存储 '@odata.count'。 ?

您可以使用 tap 来挖掘可观察值的价值。

getAllBankAccounts(url: string): Observable<BankAccount[]> {
  return this.http.get(url).pipe(tap(res=>console.log(res['@odata.count'])),map((res) => res['value']));
}

将@odata.count 保存到本地存储尝试使用这个新版本作为最后的答案

getAllBankAccounts(url: string): Observable<BankAccount[]> {
  return this.http.get(url).pipe(tap(res=>localStorage.setItem('odata',res['@odata.count'])),map((res) =>{ 
//here do anything 
res['value']}));
}