Arrays 2个参数上的typescript筛选器数组
为了在可能的2个参数上过滤数组,我编写了以下代码:Arrays 2个参数上的typescript筛选器数组,arrays,typescript,filter,Arrays,Typescript,Filter,为了在可能的2个参数上过滤数组,我编写了以下代码: filterStudies(searchString?: string) { if (searchString && !this.selectedModalityType) { this.studies = this.fullStudyList.filter(function (study) { return (study.Code.toUpperCase(
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())
})
错误消息:错误类型错误:无法读取未定义的属性“selectedModalityType”,我看到它实际上已记录在前面的行中
我错过了什么
谢谢,在您的功能中,
这与前一行不同
这将有助于:
let self = this;
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === self.selectedModalityType.toUpperCase())
})
您可以阅读以下内容了解更多信息:
JavaScript中的this关键字(以及TypeScript)的行为与许多其他语言中的不同。这可能是非常令人惊讶的,特别是对于其他语言的用户来说,他们对这应该如何工作有一定的直觉。
(...)
在这种情况下,丢失信息的典型症状包括:
- 当需要其他值时,类字段(this.foo)未定义