Consider this Angular component using RxJS filter operator to select values:
import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-filter-demo',
standalone: true,
template: `<div>Filtered Values: {{ filteredValues() }}</div>`
})
export class FilterDemoComponent {
filteredValues = signal([]);
constructor() {
const source$ = from(['apple', 'banana', 'cherry', 'date']);
source$.pipe(
filter(fruit => fruit.length > 5)
).subscribe(value => {
this.filteredValues.update(arr => [...arr, value]);
});
}
} What will be displayed inside the <div> after component initialization?
import { Component, signal } from '@angular/core'; import { from } from 'rxjs'; import { filter } from 'rxjs/operators'; @Component({ selector: 'app-filter-demo', standalone: true, template: `<div>Filtered Values: {{ filteredValues() }}</div>` }) export class FilterDemoComponent { filteredValues = signal<string[]>([]); constructor() { const source$ = from(['apple', 'banana', 'cherry', 'date']); source$.pipe( filter(fruit => fruit.length > 5) ).subscribe(value => { this.filteredValues.update(arr => [...arr, value]); }); } }
Think about which fruits have names longer than 5 characters.
The filter operator selects only fruits with length greater than 5. 'banana' and 'cherry' have 6 letters, so they pass the filter. 'apple' and 'date' do not.
Choose the correct syntax to filter emitted values from an observable numbers$ to only even numbers.
import { filter } from 'rxjs/operators'; numbers$.pipe( // filter condition here ).subscribe(console.log);
Remember the modulo operator % returns the remainder.
Option A correctly uses n % 2 === 0 to check if a number is even. Option A uses division incorrectly, C uses assignment instead of comparison, and D uses bitwise AND incorrectly.
Look at this code snippet:
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';
const source$ = from([1, 2, 3, 4]);
source$.pipe(
filter(x => x > 5)
).subscribe(console.log);Why does it not print anything?
import { from } from 'rxjs'; import { filter } from 'rxjs/operators'; const source$ = from([1, 2, 3, 4]); source$.pipe( filter(x => x > 5) ).subscribe(console.log);
Check the condition inside the filter and the source array values.
The filter condition x > 5 excludes all numbers since none in [1,2,3,4] are greater than 5. So no values are emitted.
Given this Angular component snippet:
import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-signal-filter',
standalone: true,
template: `<div>Count: {{ count() }}</div>`
})
export class SignalFilterComponent {
count = signal(0);
constructor() {
const source$ = from([10, 15, 20, 25]);
source$.pipe(
filter(x => x % 10 === 0)
).subscribe(() => {
this.count.update(c => c + 1);
});
}
}What will be the value of count() after subscription completes?
import { Component, signal } from '@angular/core'; import { from } from 'rxjs'; import { filter } from 'rxjs/operators'; @Component({ selector: 'app-signal-filter', standalone: true, template: `<div>Count: {{ count() }}</div>` }) export class SignalFilterComponent { count = signal(0); constructor() { const source$ = from([10, 15, 20, 25]); source$.pipe( filter(x => x % 10 === 0) ).subscribe(() => { this.count.update(c => c + 1); }); } }
Count how many numbers in the array are divisible by 10.
Only 10 and 20 satisfy x % 10 === 0. So the count increments twice, resulting in 2.
Choose the most accurate description of how the filter operator works in Angular RxJS streams.
Think about whether filter changes values or just decides which to pass through.
The filter operator does not change values; it only lets through values that meet the condition, ignoring others.