Create Observable From String, Array And Object In Angular
In this article, we will learn how to make an observable in Angular using the create, of, and from operators. They can be used to make new observables out of an array, string, object, collection, or any other static data. Also, understand the distinction between the Of and From operators. If you're unfamiliar with observables, we recommend starting with the Angular observable.
Observable creation functions
In Angular, there are a variety of ways to make an observable. As taught in the observable lesson, you can use Observable Constructor. There are several functions that can be used to create new observables. These operators allow us to generate observables from arrays, strings, promises, iterables, and other data types. Some of the operators are listed below.
- create
- defer
- empty
- from
- fromEvent
- interval
- of
- range
- throw
- timer
The RxJs core library includes all creation-related operations. It's available in the 'rxjs' library.
Create
The Create method is one of the most straightforward. Behind the scenes, the create method calls the observable constructor. You don't need to import Create because it's a method of the observable object.
ngOnInit() { //Observable from Create Method const obsUsingCreate = Observable.create( observer => { observer.next( '1' ) observer.next( '2' ) observer.next( '3' ) observer.complete() }) obsUsingCreate .subscribe(val => console.log(val), error=> console.log("error"), () => console.log("complete")) } ****Output ***** 1 2 3 Complete
Observable Constructor
ngOnInit() { //Observable Using Constructor const obsUsingConstructor = new Observable( observer => { observer.next( '1' ) observer.next( '2' ) observer.next( '3' ) observer.complete() }) obsUsingConstructor .subscribe(val => console.log(val), error=> console.log("error"), () => console.log("complete")) } ****Output ***** 1 2 3 complete
Of Operator
import { of } from 'rxjs';
observable from an array
ngOnInit() { const array=[1,2,3,4,5,6,7] const obsof1=of(array); obsof1.subscribe(val => console.log(val), error=> console.log("error"), () => console.log("complete")) } **** Output *** [1, 2, 3, 4, 5, 6, 7] complete
More than one array can be sent.
ngOnInit() { const array1=[1,2,3,4,5,6,7] const array2=['a','b','c','d','e','f','g'] const obsof2=of(array1,array2 ); obsof2.subscribe(val => console.log(val), error=> console.log("error"), () => console.log("complete")) } **** Output *** [1, 2, 3, 4, 5, 6, 7] ['a','b','c','d','e','f','g'] complete
observable from a sequence of numbers
ngOnInit() { const obsof3 = of(1, 2, 3); obsof3.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } **** Output *** 1 2 3 complete
observable from string
ngOnInit() { const obsof4 = of('Hello', 'World'); obsof4.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } **** Output *** Hello World complete
observable from a value, array & string
ngOnInit() { const obsof5 = of(100, [1, 2, 3, 4, 5, 6, 7],"Hello World"); obsof5.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } **** Output *** 100 [1, 2, 3, 4, 5, 6, 7] Hello World complete
From Operator
- an Array,
- anything that behaves like an array
- Promise
- any iterable object
- collections
- any observable object
import { from } from 'rxjs';
observable from an array
ngOnInit() { const array3 = [1, 2, 3, 4, 5, 6, 7] const obsfrom1 = from(array3); obsfrom1.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } *** Output **** 1 2 3 4 5 6 7 complete
Observable from string
ngOnInit() { const obsfrom2 = from('Hello World'); obsfrom2.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } *** Output **** H e l l o W o r l d complete
Observable from collection
ngOnInit() { let myMap = new Map() myMap.set(0, 'Hello') myMap.set(1, 'World') const obsFrom3 = from(myMap); obsFrom3.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) ) *** output *** [0, "Hello"] [1, "World"] complete
Observable from iterable
ngOnInit() { const obsFrom4 = from(this.generateNos()) obsFrom4.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } *generateNos() { var i = 0; while (i < 5) { i = i + 1; yield i; } *** Output *** 1 2 3 4 5
Observable from promise
ngOnInit() { const promiseSource = from(new Promise(resolve => resolve('Hello World!'))); const obsFrom5 = from(promiseSource); obsFrom5.subscribe(val => console.log(val), error => console.log("error"), () => console.log("complete")) } *** Output **** Hello World complete
Of Vs From
Of | from |
---|---|
Accepts variable no of arguments | Accepts only one argument |
emits each argument as it is without changing anything | iterates over the argument and emits each value |