Arrays &引用;category.forEach不是一个函数;
我试图在下拉列表中显示Arrays &引用;category.forEach不是一个函数;,arrays,angular,typescript,foreach,Arrays,Angular,Typescript,Foreach,我试图在下拉列表中显示this.cat,但出现以下错误: category.forEach不是一个函数 这是我的代码: getCategories(){ this.policeService.getCategoriesList().subscribe( data => { this.categories=data; console.log(this.categories); }, error => c
this.cat
,但出现以下错误:
category.forEach不是一个函数
这是我的代码:
getCategories(){
this.policeService.getCategoriesList().subscribe(
data => {
this.categories=data;
console.log(this.categories);
},
error =>
console.log(error));
}
changeCategories() {
this.sourceCategories.length = 0;
this.categories.forEach((categorie) => {
categorie.forEach((categorie) => this.brancheid = categorie.branche.id);
if(this.policeform.get("branche").value.id == this.brancheid) {
this.sourceCategories.push(categorie);
this.cat = JSON.parse(JSON.stringify(this.sourceCategories));
}
})
}
您的类别数组看起来像是对象形式的数组 尝试数组原型forEach方法。。它会起作用的
Array.prototype.forEach.call(this.categories, categorie=> {
console.log(categorie)
});
分类中的每一个都不需要
changeCategories(){
this.sourceCategories.length=0;
this.categories.forEach((categorie)=>{
this.brancheid=categorie.branche.id;
if(this.policeform.get("branche").value.id==this.brancheid){
this.sourceCategories.push(categorie);
this.cat=JSON.parse(JSON.stringify(this.sourceCategories));
}
})
}
categories
中的数据是什么?categorie
不是一个可编辑的对象或数组。您在哪里定义了categories列表?也请将categories示例置于问题中。并且不要给每个元素起相同的名字,比如有问题的分类类别数组。我真的很想知道这是如何解决你的可观察问题的,以及和第一个循环的区别@拉米亚拉克