Array.Filter和找不到错误角度4

我有一个列表,我只是想过滤。 Array.find和filter都会产生错误。Array.Filter和找不到错误角度4

功能:

setSupplierDetails(supplierId) { 

const supplier = this.suppliers.filter(tempSupplier => tempSupplier.id === supplierId)[0];

this.supplierName = supplier.supplierName;

} 错误:

ERROR TypeError: Cannot read property 'filter' of undefined 

at AddEditRestockComponent.webpackJsonp.../../../../../src/app/restock/add-edit-restock/add-edit-restock.component.ts.AddEditRestockComponent.setProductRelatedInfo

回答:

如果你的数组没有初始化,这应该发生,以处理initalize您的阵列

suppliers: SomeClass[] = []; 

和内方法

setSupplierDetails(supplierId) { 

if(this.suppliers && this.suppliers.length > 0){

const supplier = this.suppliers.filter(tempSupplier => tempSupplier.id === supplierId)[0];

}

以上是 Array.Filter和找不到错误角度4 的全部内容, 来源链接: utcz.com/qa/266579.html

回到顶部