簡體   English   中英

map 是否可能超過另一個 map 結果,rxjs?

[英]Possible to map over another map result, rxjs?

我正在使用 angular 7 並且在理解 pipe 語法的工作方式時遇到了一些問題。

到目前為止,這是我的代碼:

    tests: Row[]
    public getTest() : Observable<Row[]> {
      return this.http.get<Test[]>(this.testUrl)
      .pipe(
        map((tests) => {
          tests.map(test => test.rows = this.tests)
          return this.tests;
    }))
  }

這是我的 model:

export interface Test {
  total_rows: number;
  offset: number;
  rows: Row[];
}

export interface Row {
  id: string;
  key: Key;
  value?: any;
}

export interface Key {
  name: string;
}

這是 JSON:

{
  "total_rows": 2,
  "offset": 0,
  "rows": [
    {
      "id": "54229d6897e1d1c7d603428a850081d5",
      "key": {
        "name": "test1"
      },
      "value": null
    },
    {
      "id": "54229d6897e1d1c7d603428a85010e03",
      "key": {
        "name": "test2"
      },
      "value": null
    }
  ]
}

我想得到的數據是:一個行數組

謝謝您的幫助

在您的示例中,一種方法屬於Observal.map() ,另一種方法是原始原生Javascript 數組.map()

不確定您要實現什么,如果您只想檢索Rows數組並將其分配給this.tests ,您可以簡單地使用Observable.map()轉換Observable而無需使用 JS 數組.map() 記得訂閱你的 observable:

public getTest() : Observable<Row[]> {
  return this.http.get<Test[]>(this.testUrl)
  .pipe(
    map((tests) =>  tests.rows)))

//remember to subscribe
this.getTest().subscribe(results => this.tests = results) //assign the rows to local this.tests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM