Consider this Angular component that uses RxJS map operator to transform data from an observable. What will be displayed in the template?
import { Component } from '@angular/core'; import { of } from 'rxjs'; import { map } from 'rxjs/operators'; @Component({ selector: 'app-transform', template: `<div>{{ transformedValue | async }}</div>` }) export class TransformComponent { source$ = of(5); transformedValue = this.source$.pipe( map(x => x * 3) ); }
Remember that map transforms the emitted value before it reaches the template.
The map operator multiplies the emitted value 5 by 3, resulting in 15. The async pipe unwraps the observable and displays 15.
Given an observable emitting strings, which code snippet correctly uses map to transform each string to its length?
import { of } from 'rxjs'; import { map } from 'rxjs/operators'; const source$ = of('apple', 'banana', 'cherry'); const lengths$ = source$.pipe( // transformation here );
Check how to get the length of a string in JavaScript.
Option A correctly accesses the length property of the string. Option A tries to call length() as a function, which is invalid. Option A transforms to uppercase, not length. Option A adds 1 to length, which is incorrect.
Examine the following code snippet. Why does it cause a runtime error when subscribed?
import { of } from 'rxjs'; import { map } from 'rxjs/operators'; const source$ = of(null); const transformed$ = source$.pipe( map(x => x.toString()) ); transformed$.subscribe(console.log);
Think about what happens when you call a method on null.
The observable emits null. Calling toString() on null causes a runtime TypeError because null has no methods.
Given this observable pipeline, what is the final value emitted?
import { of } from 'rxjs'; import { map } from 'rxjs/operators'; const source$ = of(2); const result$ = source$.pipe( map(x => x + 3), map(x => x * 2), map(x => `Value: ${x}`) ); result$.subscribe(console.log);
Apply each map transformation step by step.
Starting with 2, add 3 → 5, multiply by 2 → 10, then format as string → 'Value: 10'.
Choose the most accurate description of how the map operator works in Angular's RxJS library.
Recall the purpose of map in functional programming and RxJS.
The map operator applies a function to each value emitted by the source observable and emits the result. Other options describe different operators like filter, merge, and delay.