带有RxJS observable的JSON数据:过滤器不起作用



首先:Plunkr的例子是这里的<-- NOW: WORKING EXAMPLE (edited)

这是数据:

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

这是过滤器:

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

我想得到的结果:

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

也许我停电了,哪里出错了?

在这种情况下可以使用.concatMap

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

Example

最新更新