Home > database >  How to change object of an observable in angular?
How to change object of an observable in angular?

Time:12-09

I have an observable in witch I get data from server and I need on init to filtre this data and use it in project. How we can do this in angular ?

//get data from server
this.sites = _httpClient.get<IModel[]>('api/mysites');
  ngOnInit(): void {
   //there I show data from server
   this.sites.subscribe(x=> console.log('first console', x));
   
   //and there I need to have filtred data 
   this.sites.subscribe(x=> console.log('second console', x));

}

CodePudding user response:

If you need to filter the data, your best bet would be to use the filter pipe:

ngOnInit(): void {
  this.sites
   .pipe(filter(x => // filter data here))
   .subscribe(x=> console.log('first console', x));

  // 'first console' will print your filtered data
}

CodePudding user response:

You can Use Pipes to transform your data once you get the value from the Server 
https://angular.io/guide/pipes Refer this for more information on transforming data

        this.sites = _httpClient.get<IModel[]>('api/mysites').pipe(
         
    );

      ngOnInit(): void {
       //there I show data from server
       this.sites.subscribe(x=> console.log('first console', x));
       
       //and there I need to have filtred data 
       this.sites.subscribe(x=> console.log('second console', x));

    }

  • Related