Angular可观察对象推送到数组



我使用这段代码将任务名称放入数组barChartLabels。

public lineChartLabels: Label[] = [];
this.tasks.pipe(map(x => x.map(x => x.id))).toPromise().then(id => this.lineChartLabels.push(id))

代码仍在工作,但有一个小问题。事实上,它将所有名称复制到lineChartLabels的第一个索引中。我希望每个名称都写在它自己的数组索引中。如何更改代码以将每个名称放在单个索引中?

  1. 没有必要将可观察对象转换为promise。事实上,toPromise()在RxJS v8中已经停止了。用subscribe代替。

  2. 不需要将数组压入另一个数组,只需要对它进行赋值。

public barChartLabels: Label[] = [];
this.tasks.pipe(
map(t => t.map(t => t.name))
).subscribe(
names => this.barChartLabels = names
);

最新更新