ToArray Operator - RXJS

ToArray Operator - RXJS

In this tutorial, we will learn about the toArray() operator of RXJS.

ToArray() Operator

The ToArray operator Collects all source emissions and emits them as an array when the source completes.

This is basically Used to transform data.

Which used before subscribe using pipe

Example -1:

import { toArray} from "rxjs/operators";
-----------------
users = [
    { name: "Jigar", skill: "Angular" },
    { name: "Jig", skill: "Node" },
    { name: "Jir", skill: "CSS" },
  ];

const source = from(this.users);

source.pipe(toArray()).subscribe((res) => {
      console.log(res);
});

Output:
[
    { name: "Jigar", skill: "Angular" },
    { name: "Jig", skill: "Node" },
    { name: "Jir", skill: "CSS" },
];

Example -2:

 const source = of("Jigar", "Jiya", "Aman");

 source3.pipe(toArray()).subscribe((res) => {
      console.log(res);
 });

OutPut:
["Jigar", "Jiya", "Aman"]

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

You might also like

Leave a Comment