类型“ any []”上不存在属性“ contactId”

时间:2019-05-02 09:02:18

标签: arrays angular typescript angular6

我正在尝试过滤一系列称为“ notes”的对象。尝试此操作时,出现错误:类型'any []'不存在属性'contactId'。

notes: Array < any > [] = [];
currentNotes: Array < any > [] = [];

notes.forEach(element => {
  //Filter out notes without contact  
  if (element.contactId != null) {
    this.currentNotes.push(element);
  }
})

2 个答案:

答案 0 :(得分:2)

您定义了数组数组,您的代码应如下所示

ElemsCombine3

答案 1 :(得分:0)

在比较之前只需检查element是否包含联系人ID

notes: Array < any > [] = [];
    currentNotes: Array < any > [] = [];

    notes.forEach(element => {
      //Filter out notes without contact  
      if (element.contactId&&element.contactId != null) {
        this.currentNotes.push(element);
      }
    })