【问题标题】:Use a variable while traversing data given by subscribe in Angular在遍历 Angular 中 subscribe 给出的数据时使用变量
【发布时间】:2020-07-29 12:42:10
【问题描述】:
getspacecol(){
this.gets=true;
this.selectedfeatures=[];
this.variantService.getData().subscribe((dat) => {
  this.dataa = dat;

   for(let i=0;i<this.selectedItems.length;i++)
   {
    this.tool=this.selectedItems[i].itemName;
    }
  console.log(this.tool);
  for(let i=0 ; i<this.dataa.length;i++){

    if(this.dataa[i].this.tool===this.searchResult.value)//error in this.tool
     {
      this.selectedfeatures.push(this.dataa[i]);               
     }      
  }
  this.data=[...this.selectedfeatures];
});
}

大家好,我的表中有 200 列,并且 angular2 多选下拉列表包含 200 列作为下拉值。我在变量 selectedItems 中获得下拉选择值 在 if 条件下,如果我给出 this.dataa[i].stackover 则没有错误,因为 stackover 是列之一。下拉列表中存在相同的 stackover,并在选择时打印在 console.log 中。

现在我需要在 this.dataa[i].this.tool 的 if 条件中使用此变量,否则我将不得不为 200 列编写 if 条件 你们能帮我解决这个问题吗

【问题讨论】:

    标签: javascript json angular typescript angular-observable


    【解决方案1】:

    在 if 循环this.dataa[i].this.tool 中,this.tool 被视为键名,它试图在您的对象中找到不正确的“this.tool”键,因此您应该使用以下语法来实现一样。

    例子:

    var keyName = this.tool
    this.dataa[i][keyName]
    

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2019-01-12
      • 2020-01-20
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2018-03-07
      相关资源
      最近更新 更多