I am new to Angular and am trying to learn by examining code samples. I pulled some sample code for ngx-Typeahead and am trying to understand the code.
interface SearchResponse {
total_count: number;
incomplete_results: boolean;
items: Owner[];
}
then
this.suggestions$ = new Observable((observer: Observer<string>) => {
observer.next(this.search);
}).pipe(
switchMap((query: string) => {
if (query) {
return this.http.get<SearchResponse>('https://localhost:5001/api/GetOwners', {
params: { q: query }
}).pipe(
map((data: SearchResponse) => data && data.items || []),
tap(() => noop, err => { this.errorMessage = err && err.message || 'Something goes wrong'; })
);
}
return of([]);
})
);
The line of code that says:
map((data: SearchResponse) => data && data.items || [])
Reads something like "map the response into this function and call it "data". Then with this, AND it with the items (an element of SearchResponse). If that AND is nothing, then return an empty array." So what if it isn't nothing? What does "data && data.items yield? Or am I overthinking this and data && data.items will return true so the whole data item is returned?