Search code examples
dictionaryrxjspipeobservableangular7

Possible to map over another map result, rxjs?


I'm getting on angular 7 and having some issues understanding the way pipe syntax is working.

Here is my code so far :

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

Here is my 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;
}

Here is the JSON :

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

And the data I would like to get is : an array of Rows

Thank you for your help


Solution

  • In your example, one method belongs to the Observal.map() and the other one is original native Javascript's array .map().

    Not sure what you are trying to achieve, if you just want to retrieve the array of Rows and assign it to this.tests, you can simply tranform the Observable using Observable.map() and there is no need to use JS array .map(). Remember to subscribe to your 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