我的 json 檔案包含幾個陣列,如下所示:
{
A[]
B[]
C[]
...
}
我的查詢如下所示:
myFunction():void{
this.apiService.getData()
.pipe(
map((response: any) => response.A), // to access to the 'A' array
take(1)
)
.subscribe((records: MyType[]) => {
this.records = records
});
}
此查詢對于每個陣列都是相同的。只需用正確的陣列名稱(B,C)更改“A”。
我不知道這是否可能,但我需要的只是將一個變數傳遞給函式并有一個“switch case”來重定向到正確的表。
我看著 switchMap 運算子,但它似乎不是正確的解決方案。
任何建議都是hepfull
謝謝
uj5u.com熱心網友回復:
如果我理解正確的問題,看起來你可能會選擇這樣的東西
myFunction(arrayName: string):void{
this.apiService.getData()
.pipe(
map((response: any) => response[arrayName]),
// you do not need take(1) if the apiService wraps the http client
// http client notifies one value and then completes immediately after
)
.subscribe((records: MyType[]) => {
this.records = records
});
}
uj5u.com熱心網友回復:
這個解決方案似乎有效,但我不確定它是否是最好的。
myFunction(prov: string): void{
this.jsonService.getData()
.pipe(
map((response: any) =>
prov === "A" ? response.A
: prov === "B" ? response.B
: prov === "C" ? response.C
: ...
: response.Z
)
)
.subscribe((record: any) =>
this.records = records
)
}
但是 Picci 的解決方案是最簡單的!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/427222.html
上一篇:reduce方法未通過單元測驗
下一篇:單擊按鈕時如何關閉選定的div
