Vienna Vienna - 3 months ago 45
JSON Question

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?

Answer

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

Comments