【发布时间】:2019-10-08 04:16:46
【问题描述】:
我正在读取一个 json 数组,然后将其作为可观察对象发送回几个函数。我正在尝试过滤这是其中的两个函数,但它不起作用并且它返回一个空值。
过滤这个的正确方法是什么?
我尝试了几种不同的返回方式,但它总是返回空值。
这个返回所有数据并实际工作
console.log('getMenu1');
return this.http.get('../../assets/data/hospitals.json').pipe(
map(results => results['hospitals'])
);
}
这是应该根据搜索字符串过滤名称的函数之一,但它不起作用....总是为空
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 ) )
);
}
这应该返回一个完全匹配的 id,但它不起作用 - 也是 null。
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.
【问题讨论】:
标签: angular ionic-framework filter observable