【问题标题】: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);
            }
          })
      

      【讨论】:

        猜你喜欢
        • 2021-02-08
        • 2018-09-09
        • 2019-12-31
        • 2022-12-17
        • 2021-11-29
        • 1970-01-01
        • 2017-10-20
        • 2017-01-14
        • 2020-01-11
        相关资源
        最近更新 更多