首页 > 解决方案 > 类型“any[]”上不存在属性“contactId”

问题描述

我正在尝试过滤一个名为“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);
  }
})

标签: arraysangulartypescriptangular6

解决方案


你定义数组数组,你的代码应该是这样的

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

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

推荐阅读