【问题标题】:How to filter an observable object in ionic 4 / angular 7?如何过滤离子 4 / 角度 7 中的可观察对象?
【发布时间】: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


    【解决方案1】:

    您的代码中有一些错误。

    indexOf(searchText) &gt; 0 有两个错误。您应该使用searchText.toLowerCase() 确保searchText 也是小写。您还需要将0 更改为-1,否则会错过搜索文本开头的搜索字符串。

    getHospitalByID 函数在我测试时对我有用,请参阅 this working StackBlitz 应用程序。

    【讨论】:

    • 我确实弄明白了,但我的评论中没有足够的空间来分享代码抱歉。
    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多