从 Angular 中的另一个数组推送 ArrayItem

Push ArrayItem from another array in Angular

我处于需要从另一个数组中推送数组项的情况。

export class Products {
    ProductCat: string;
    Products: number;
    Sold: number;
}

export class ProdCats {
    Id: number;
    Category: string;
}

prodCats = ProdCats[];
products Products[];

//I get all the product categories here
this.prodCats = await this.service.getProdCats().toPromise();

now I want to fill my "products" array like this:
for (let i = 0; i < this.prodCats.length; i++) {
  var item = {
    "ProductCat": this.prodCats[i].Category,
    "Products": 0, //want to add default value 0 later on I will update these
    "Sold": 0 //want to add default value 0 later on I will update these
  };
  this.products.push(item);
}

我不知道这种方法是否有效,但它对我来说失败并出现错误:

core.js:5882 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined

请告诉我如何以有效的方式实现这一点。

您没有创建 products 数组。这样做:

products: Products[] = [];