Home > other >  How get two or more values as an array from observable rxjs?
How get two or more values as an array from observable rxjs?

Time:03-21

I have btn event:

 checkAnswer() {

 const btn = this.btns.map(btn => btn.nativeElement);

 this.nextAskSub = fromEvent(btn, 'click').pipe(

*// Surely this is wrong, can something like this be done? //*

 [pluck('target', 'value') && pluck('target', 'disabled')]

).subscribe((res: any) => {
  this.data = res[0]
  this.disabled = res[1]
})

}

I want to return btn value and disabled status as an array. Is it possible?

CodePudding user response:

You could just use map:

this.nextAskSub = fromEvent(btn, 'click').pipe(
    map(e => [e.target.value, e.target.disabled])
).subscribe((res: any) => {
  this.data = res[0]
  this.disabled = res[1]
})

Just basically mapping the event to a "tuple" of two elements.

  • Related