RxJS 面试题与答案
问题 6. Explain the difference between 'map' and 'mergeMap' operators in RxJS.
'map' is used to transform values emitted by an Observable, while 'mergeMap' is used to project each value to an Observable and flatten the resulting Observables into one.
Example:
const modifiedObservable = observable.pipe(mergeMap(value => of(value, value * 2)));
问题 7. What is the purpose of the 'filter' operator in RxJS?
The 'filter' operator is used to selectively emit values from an Observable based on a provided predicate function.
Example:
const filteredObservable = observable.pipe(filter(value => value > 5));
问题 8. Explain the concept of multicasting in RxJS.
Multicasting is the process of broadcasting a single source Observable to multiple subscribers, preventing redundant work for shared operations.
Example:
const subject = new Subject(); observable.subscribe(subject);
问题 9. What is the difference between 'Cold' and 'Hot' Observables?
'Cold' Observables start producing values only when a subscription is made, while 'Hot' Observables produce values even before any subscriptions.
Example:
Cold: const coldObservable = new Observable(...); Hot: const hotObservable = new Subject();
问题 10. Explain the purpose of the 'debounceTime' operator in RxJS.
The 'debounceTime' operator is used to emit a value from an Observable only after a specified amount of time has passed without any new values being emitted.
Example:
const debouncedObservable = inputObservable.pipe(debounceTime(300));
用户评价最有帮助的内容: