angular - 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
和指定的按钮protection
,checkAdded()
则将正常工作。
但是如果类型是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'
吗?但是出于同样的原因,代码就像使用多种方法一样。
解决方案
推荐阅读
- docker - 使用 CircleCI 的预建镜像与拉取私有镜像之间的权衡是什么?
- android - styles.xml 未按预期应用
- docker - Docker容器到容器连接:连接被拒绝
- java - 如何通过在 java-sql 应用程序上输入所有列数据来过滤搜索?
- c - 如何在 C 代码中使用单精度浮点实现 FIR 滤波器?
- tcp - 使用 netstat -s 在 Solaris 10 中计算 TCP 重传
- python - 读取超时。向 node.js API 发送 POST 请求时出错
- android - My App crashes when I try to use ArrayAdapter
- reactjs - mapStateToProps returns no data, data is undefined
- hyperlink - Item Tree not being populated from link selector in Sitecore 9.2