如何过滤离子4 /角度7中的可观察对象?

时间:2019-05-21 17:50:34

标签: angular ionic-framework filter observable

我正在读取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.

1 个答案:

答案 0 :(得分:0)

您的代码中有一些错误。

indexOf(searchText) > 0有两个错误。您应该使用searchText确保searchText.toLowerCase()也是小写。您还需要从0更改为-1,因为否则它将丢失位于搜索文本开头的搜索字符串。

getHospitalByID功能在我测试时对我有用,请参见this working StackBlitz应用。

相关问题