类型为typescript for for循环本地循环变量不尊重类型

时间:2017-11-29 20:35:34

标签: typescript types

this.venuelist有类型Venue

venuelist:Venue[] = [];

类型Venue上有属性neighborhood

我有以下for循环

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

错误是:'string'类型中不存在属性'邻域'。

为什么?为什么必须这样做。为什么它不能玩得好听?我如何让它变得更好?

2 个答案:

答案 0 :(得分:1)

for(let venue in this.venuelist){}

for ... in doesn迭代数组项,它迭代传入的对象的键。你应该使用for...of

答案 1 :(得分:0)

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