Я читаю в массиве json, а затем отправляю его обратно в несколько функций в качестве наблюдаемой. Я пытаюсь отфильтровать это две функции, но они не работают и возвращают нулевое значение.
Как правильно отфильтровать это?
Я пробовал несколько разных способов его возврата, но он всегда возвращает нули.
Этот возвращает все данные и на самом деле работает
console.log('getMenu1');
return this.http.get('../../assets/data/hospitals.json').pipe(
map(results => results['hospitals'])
);
}
Это одна из функций, которая должна фильтровать имя на основе строки поиска, но она не работает .... всегда получает значение null
getFilteredHospitals1(text: string): Observable<any> {
console.log('getMenu1');
let searchText = text.toLowerCase();
return this.http.get('../../assets/data/hospitals.json').pipe(
map(results => results['hospitals'].filter(res => res.name.toString().toLowerCase().indexOf(searchText) > 0 ) )
);
}
Предполагается, что он возвращает точное совпадение для идентификатора, но это не работает - также ноль.
getHospitalByID(id: string): Observable<any> {
console.log('getHospitalByID = ' + id);
let searchText = id.toLowerCase();
console.log('getHospitalByID = ' + searchText);
return this.http.get('../../assets/data/hospitals.json').pipe(
map(results => results['hospitals'].filter(res => res.id === searchText ) )
);
}
Это файл json, который он читает
{
"hospitals": [ {
"id": "1",
"name": "Lehigh Valley Hospital-Cedar Crest",
"address": "1200 South Cedar Crest Blvd, Allentown, PA 18105",
"website": "www.lvhn.org",
"trauma": "Level I",
"peds": "Level II",
"stroke": "Comprehensive",
"ebola": "No",
"phone": "610-402-8000",
"special": "Special Annodote",
"speciallink": "www.lvhn.org"
},
{
"id": "2",
"name": "Lehigh Valley Hospital-17th Street",
"address": "17th and Chew Sts, Allentown, PA 18105",
"website": "www.lvhn.org",
"trauma": "N/A",
"peds": "N/A",
"stroke": "N/A",
"ebola": "No",
"phone": "610-969-2388",
"special": "Special Annodote",
"speciallink": "www.lvhn.org"
},
{
"id": "3",
"name": "Lehigh Valley Hospital-Mulenberg",
"address": "2545 Schoenersville Rd, Bethlehem, PA 18017",
"website": "www.lvhn.org",
"trauma": "N/A",
"peds": "N/A",
"stroke": "Primary",
"ebola": "Yes",
"phone": "610-402-8000",
"special": "Special Annodote",
"speciallink": "www.lvhn.org"
},
{
"id": "4",
"name": "Lehigh Valley Hospital-Hazleton",
"address": "700 East Broad St, Hazleton, PA 18201",
"website": "hazleton.lvhn.org",
"trauma": "N/A",
"peds": "N/A",
"stroke": "Primary",
"ebola": "No",
"phone": "570-501-4000",
"special": "Special Annodote",
"speciallink": "www.lvhn.org"
}
]
}
Здесь это называется
ngOnInit() {
// Get the ID that was passed with the URL
let id = this.activatedRoute.snapshot.paramMap.get('id');
console.log('Details');
this.hospitalService.getHospitalByID(id).subscribe(result => {
this.information = result;
});
console.log(this.information);
}```
Unfortunately, the two "search" functions always return null. The first function works.