Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays &引用;category.forEach不是一个函数;_Arrays_Angular_Typescript_Foreach - Fatal编程技术网

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示例置于问题中。并且不要给每个元素起相同的名字,比如有问题的分类类别数组。我真的很想知道这是如何解决你的可观察问题的,以及和第一个循环的区别@拉米亚拉克