【问题标题】:Property 'contactId' does not exist on type 'any[]'类型“any[]”上不存在属性“contactId”
【发布时间】:2019-09-20 18:28:44
【问题描述】:
我正在尝试过滤一个名为“notes”的 abject 数组。当我尝试这个时,我收到错误:属性 'contactId' 在类型 'any[]' 上不存在。
notes: Array < any > [] = [];
currentNotes: Array < any > [] = [];
notes.forEach(element => {
//Filter out notes without contact
if (element.contactId != null) {
this.currentNotes.push(element);
}
})
【问题讨论】:
标签:
arrays
angular
typescript
angular6
【解决方案1】:
你定义了数组数组,你的代码应该是这样的
notes: Array < any > = [];
currentNotes: Array < any > = [];
notes.forEach(element => {
//Filter out notes without contact
if (element.contactId) {
this.currentNotes.push(element);
}
})
【解决方案2】:
在比较之前检查element是否包含contactid
notes: Array < any > [] = [];
currentNotes: Array < any > [] = [];
notes.forEach(element => {
//Filter out notes without contact
if (element.contactId&&element.contactId != null) {
this.currentNotes.push(element);
}
})