Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs 6 - Observable<Array(Objects)> to Observable<Objects>

I have an observable that emits an array of objects. What pipe able operators do I need to use to transform it to an Observable so I can act on each object?

What do I need to do to obs$ to make it emit like obs2$ ?

const obs$ = of([{ opponent: 'Walton', team: 'varsity', gametime: new Date() },
{ opponent: 'Scott', team: 'varsity', gametime: new Date() },
{ opponent: 'Dixie', team: 'varsity', gametime: new Date() },
{ opponent: 'Highlands', team: 'freshmen', gametime: new Date() }])
  .pipe(
    tap(console.log)
  );

obs$.subscribe(a =>
  console.log(a)
);

const obs2$ = of({ opponent: 'Walton', team: 'varsity', gametime: new Date() },
  { opponent: 'Scott', team: 'varsity', gametime: new Date() },
  { opponent: 'Dixie', team: 'varsity', gametime: new Date() },
  { opponent: 'Highlands', team: 'freshmen', gametime: new Date() })
  .pipe(
    tap(console.log)
  );

obs2$.subscribe(a =>
  console.log(a)
);
like image 475
user1247395 Avatar asked Mar 06 '23 02:03

user1247395


1 Answers

You need mergeAll:

of([2, 3, 4]).pipe(
  mergeAll()
).subscribe(v => console.log(v));
// outputs 2, 3, 4

If you use from it will work too:

from([2, 3, 4])
  .subscribe(v => console.log(v));
// outputs 2, 3, 4
like image 99
Fabricio Avatar answered Mar 11 '23 12:03

Fabricio