Moshe Yamini
Moshe Yamini

Reputation: 735

RXJS: Iterate array with click event

I have an observable with array. I need to make some operation on each element in the array on mouse click event. I started with something like this:

merge([
 this.clicks,
 this.array$
 ]).pipe(
   tap((value) => console.log(value))
 ).subscribe();

But how can i iterate the elements, so in each click it will print the next element in the array?

Upvotes: 0

Views: 225

Answers (1)

Rafi Henig
Rafi Henig

Reputation: 6424

How about the following?

arrayObservable = of(["how", "are", "you"]);

fromEvent(window, "click").pipe(
  switchMapTo(arrayObservable$),
  map((array, index) => {
    return array[index % array.length]
  })
)

Hope it helps!

Upvotes: 1

Related Questions