feat(app): add user age filtering and average calculation
This commit is contained in:
		
					parent
					
						
							
								f5673e0c24
							
						
					
				
			
			
				commit
				
					
						144d86c0d9
					
				
			
		
					 1 changed files with 32 additions and 7 deletions
				
			
		|  | @ -5,7 +5,12 @@ import { AsyncPipe, UpperCasePipe } from '@angular/common'; | ||||||
| import { TextPipe } from '../text.pipe'; | import { TextPipe } from '../text.pipe'; | ||||||
| import { HotelService } from './Parent/services/hotel.service'; | import { HotelService } from './Parent/services/hotel.service'; | ||||||
| import { inject } from '@angular/core'; | import { inject } from '@angular/core'; | ||||||
| import { filter, from, map, Observable, range, tap, toArray } from 'rxjs'; | import { filter, from, last, map, Observable, range, scan, tap, toArray } from 'rxjs'; | ||||||
|  | 
 | ||||||
|  | interface User { | ||||||
|  |   name: string; | ||||||
|  |   age: number; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-root', |   selector: 'app-root', | ||||||
|  | @ -20,15 +25,35 @@ export class AppComponent { | ||||||
|   public hotelService: HotelService = inject(HotelService); |   public hotelService: HotelService = inject(HotelService); | ||||||
| 
 | 
 | ||||||
|   ngOnInit() { |   ngOnInit() { | ||||||
|     const stream: Observable<number | string> = from([5, 1, 2, 12, 5, 14, 17, 5, "testing"]); | 
 | ||||||
|  |     const users = [ | ||||||
|  |       { name: "Max", age: 21 }, | ||||||
|  |       { name: "Peter", age: 31 }, | ||||||
|  |       { name: "Hans", age: 13 }, | ||||||
|  |       { name: "Klaus", age: 51 }, | ||||||
|  |       { name: "Dieter", age: 1 }, | ||||||
|  |       { name: "Jan-Marlon", age: 3 }, | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     const stream: Observable<User> = from(users); | ||||||
| 
 | 
 | ||||||
|     stream.pipe( |     stream.pipe( | ||||||
|       filter((value) => typeof value === "number"), |       filter((user) => user.age > 18), | ||||||
|       tap((value) => console.log("Zahl:" + value)), |       scan((acc, user) => acc + user.age, 0), | ||||||
|       filter((value: number) => value % 2 === 0), |       map((ageSum, index) => ageSum / (index + 1)), | ||||||
|       tap((value) => console.log("Gerade Zahl: " + value)), |       last(), | ||||||
|       toArray(), |  | ||||||
|     ).subscribe(console.log); |     ).subscribe(console.log); | ||||||
|  | 
 | ||||||
|  |     // const stream: Observable<number | string> = from([5, 1, 2, 12, 5, 14, 17, 5, "testing"]);
 | ||||||
|  | 
 | ||||||
|  |     // stream.pipe(
 | ||||||
|  |     //  filter((value) => typeof value === "number"),
 | ||||||
|  |     //  tap((value) => console.log("Zahl:" + value)),
 | ||||||
|  |     //  filter((value: number) => value % 2 === 0),
 | ||||||
|  |     //  tap((value) => console.log("Gerade Zahl: " + value)),
 | ||||||
|  |     //  toArray(),
 | ||||||
|  |     //).subscribe(console.log);
 | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public test() { |   public test() { | ||||||
|  |  | ||||||
		Reference in a new issue