【问题标题】:JSON Data with RxJS Observables: Filter not working带有 RxJS Observables 的 JSON 数据:过滤器不起作用
【发布时间】:2017-01-06 20:02:20
【问题描述】:

首先:Plunkr Example is here<-- 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
}

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

【问题讨论】:

  • 尝试控制台登录过滤器只是为了检查您是否真的收到了

标签: json angular filter rxjs


【解决方案1】:

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

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

Example

【讨论】:

    猜你喜欢
    • 2019-03-03
    • 2017-03-05
    • 2014-12-06
    • 2020-09-05
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多