【发布时间】:2017-09-10 11:25:24
【问题描述】:
为了过滤可能包含 2 个参数的数组,我编写了以下代码:
filterStudies(searchString?: string) {
if (searchString && !this.selectedModalityType) {
this.studies = this.fullStudyList.filter(function (study) {
return (study.Code.toUpperCase().includes(searchString.toUpperCase())) ||
(study.Description.toUpperCase().includes(searchString.toUpperCase()));
})
} else if (!searchString && this.selectedModalityType) {
console.log(this.selectedModalityType)
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
} else if (searchString && this.selectedModalityType) {
this.studies = this.fullStudyList.filter(function (study) {
return (study.Code.toUpperCase().includes(searchString.toUpperCase())) ||
(study.Description.toUpperCase().includes(searchString.toUpperCase())) &&
(study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
}
}
filterStudies(searchString?: string) 在输入文本框时调用。
另一种过滤方式是从下拉框中选择一个值。通过这段代码实现:
handleSelection(value:any){
this.selectedModalityType = value;
console.log(value)
this.filterStudies()
}
在点击此代码之前一切正常:
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
错误信息:ERROR TypeError: Cannot read property 'selectedModalityType' of undefined,我看到它实际上是在之前的行中记录的。
我错过了什么??
谢谢,
【问题讨论】:
标签: arrays typescript filter