【问题标题】:Possible to map over another map result, rxjs?可以映射另一个映射结果,rxjs?
【发布时间】:2019-10-02 16:20:31
【问题描述】:

我正在使用 Angular 7,但在理解管道语法的工作方式时遇到了一些问题。

到目前为止,这是我的代码:

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

这是我的模型:

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
    }
  ]
}

我想得到的数据是:一个行数组

感谢您的帮助

【问题讨论】:

    标签: dictionary rxjs pipe observable angular7


    【解决方案1】:

    在您的示例中,一种方法属于Observal.map(),另一种方法是原始原生Javascript's array .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
    

    【讨论】:

    • 非常感谢!我和 map() 混在一起了。
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多