Angular - 在模板中显示累积数组构建

Angular - show cumulative array building in template

如何在 Angular 模板中显示在值中构建的数组?

const timer = interval(1000);
const array = [1,2,4,6,9,10,8,9,6,5]
let newArray = []
this.count$ = timer.pipe(
    map(i => newArray.push(array[i])),
  );


<h2>{{ count$ | async }}</h2>

只显示最后发出的值。我想看 [1],然后是 [1,2],然后是 [1,2,4] 等等...

尝试使用 zipscan:

this.count$ = zip(timer, array).pipe(
  map(([i, value]) => value),
  scan((acc, cur) => [...acc, cur], [])
);

zip 将确保计时器仅发出与 array.

中的值一样多的次数

map 将去掉 timer 的值,只取 array.

的值

scan 然后会将这些值在发出时累积到一个数组中。