user2227400
user2227400

Reputation:

JSON Data with RxJS Observables: Filter not working

First of all: Plunkr Example is here <-- NOW: WORKING EXAMPLE (edited)

This is the data:

[
  {
    "label": "One",
    "value": 1
  }, 
  {
    "label": "Two",
    "value": 2
  }, 
  {
    "label": "Three",
    "value": 3
  }
]

This is the filter:

  http.get('./data.json')
  .map(data => data.json())
  .filter ( 
    (x,index) => return x[index].value === 2
  )
  .subscribe(data => this.d = data);

I would like to get as result:

{
  "label": "Two",
  "value": 2
}

Maybe I've a blackout, where is the mistake?

Upvotes: 1

Views: 1221

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

In this case you can use .concatMap

http.get('./data.json')
  .map(res => res.json())
  .concatMap(res => res)
  .filter(x => x.value === 2)
  .subscribe(data => {
    this.d = data;
  });

Example

Upvotes: 1

Related Questions