首页 > 解决方案 > Angular 7和数据源一个字段在循环内不可迭代,但如果整个数组被控制台,则可以找到

问题描述

我有以下方法返回一个数字,如果这个数字大于 0,它将禁止特定操作:

checkAdded(name, forType, type) {
  name = this.pipe.transform(name);
  let count = 0;
  if (type == "situation") {
    Object.keys(this.dataSource3).forEach((key, index) => {
      //console.log('Sit '+this.dataSource3.filteredData[index]['situation_type']+ ' '+name);
      //console.log('Sit '+this.dataSource3.filteredData[index]['situation_for']+' '+forType);
      if (this.dataSource3.filteredData[index]['situation_type'] == name && this.dataSource3.filteredData[index]['situation_for'] == forType) {
        count++;
        //console.log(count)
      }
    })
  }


    if (type == "legal") {
    Object.keys(this.dataSource2).forEach((key, index) => {
      console.log(this.dataSource2.filteredData[index]['legal_name']);
      //console.log('L '+this.dataSource2.filteredData[index]['legal_name']+ ' '+name);
      //console.log('L '+this.dataSource2.filteredData[index]['legal_for']+' '+forType);
      if (this.dataSource2.filteredData[index]['legal_name'] == name && this.dataSource2.filteredData[index]['legal_for'] == forType) {
        count++;
        console.log(count)
      }
    })
  }
  if (type == "protection") {
    Object.keys(this.dataSource).forEach((key, index) => {
      if (this.dataSource.filteredData[index]['protection_name'] == name && this.dataSource.filteredData[index]['protection_for'] == forType) {
        count++;
        //console.log(count);
      }
    })
  }
  //console.log(count)
  return count;
}

如果用户单击为 typessituation和指定的按钮protectioncheckAdded()则将正常工作。

但是如果类型是legal,添加按钮会抛出以下错误:

错误类型错误:无法读取 Array.forEach () at protection.component.ts:162 处未定义的属性“legal_name”...

数据源是:

dataSource = new MatTableDataSource<GetProtection[]>();
dataSource2 = new MatTableDataSource<GetLegal[]>();
dataSource3 = new MatTableDataSource<GetSituation[]>();

export interface GetProtection {
  protection_name: string;
  protection_date_added: string;
  protection_status: string;
  protection_for: number;

}
export interface GetLegal {
  legal_name: string;
  legal_date_added: string;
  legal_status: string;
  legal_for: number;

}
export interface GetSituation {
  situation_name: string;
  situation_date_added: string;
  situation_status: string;
  situation_for: number;

}

如果我控制台以下内容:

if (type == "legal") {
    Object.keys(this.dataSource2).forEach((key, index) => {
      console.log(this.dataSource2.filteredData[index]['legal_name']);
      if (this.dataSource2.filteredData[index]['legal_name'] == name && this.dataSource2.filteredData[index]['legal_for'] == forType) {
        count++;
        //console.log(count)
      }
    })
  }

它将迭代所有可能的值legal_name

在此处输入图像描述

所以迭代正在发生,但在if条件内部,它无法legal_name在数组中找到。

如果我在进行迭代之前进行控制台this.dataSource2,它将显示所有数组:

在此处输入图像描述.

有一种方法,dataSources在组件负载上设置所有三个:

this.dataSource = new MatTableDataSource(Object.values(data));
this.dataSource2 = new MatTableDataSource(Object.values(data2));
this.dataSource3 = new MatTableDataSource(Object.values(data3));

编辑

我可以使用.every()以防万一type == 'legal'吗?但是出于同样的原因,代码就像使用多种方法一样。

标签: angulardatasourceangular7

解决方案


推荐阅读