类型typeScript for Loop for Loop本地循环变量不尊重类型



this.venuelist有类型的地点

venuelist:Venue[] = [];

类型Venue在其上具有属性neighborhood

我有以下循环

for(let venue in this.venuelist){
      let remove = false;
      if(this.filters.neighborhood != ''){
        if(venue.neighborhood != this.filters.neighborhood){
          remove = true;
        }
      }
    }
calling venue.neighborhood called in the second `if statement` is not working

错误是:属性"邻域"在类型'字符串上不存在。

为什么?为什么必须这样做。为什么它不能很好?我该如何使其位置不错?

for(let venue in this.venuelist){}

for ... in不会迭代数组项,它在传递的对象的键上迭代。您应该使用 for...of

for(let venue in this.venuelist){
      let remove = false;
      if(this.filters.neighborhood != ''){
        if(this.venuelist[venue].neighborhood != this.filters.neighborhood){
          remove = true;
        }
      }
    }

相关内容

  • 没有找到相关文章

最新更新