我是TypeScript和Angular的初学者,所以这个问题可能会有些奇怪。 我有一个简单的JSON返回英雄的名单: http://demo8521374.mockable.io/titanAngular observable不过滤json数组的结果
我需要我的服务进行GetById但为什么总是英雄出来不确定的,可有人请点我到正确的位置,这是什么我这样做的:
return this.http.get("http://demo8521374.mockable.io/titan").map((response: Response) => {
var result = response.json().heroes as Hero[];
console.log(result);
return result;
}).filter(x => x.filter(x => x.id == id)).first().toPromise();
在我的控制台I可以看到阵列印刷但在我的成分没有得到对象:
ngOnInit(): void {
this.route.paramMap
.switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))
.subscribe(hero => this.hero = hero);
console.log(this.hero)
}
谢谢!
这是一种方法。 'Observable.filter()'正在过滤_events_(!),而不是事件中的数据。因此,在.map()中使用'.filter()'在这里是绝对正确的。 –