根据角度js中的对象创建数组

问题描述 投票:0回答:1

我是这个框架的新手。我想将对象转换为角度8的数组。但是我不知道如何工作。

实际上,我要显示状态为假的客户的多个销售交易。因此,我以某种方式获得了同一客户作为单独的不同对象进行的那些交易的价值。现在,我想将这些对象转换为单个数组,以便可以通过* ngFor在HTML文件中对其进行迭代。

  export class InvoicesComponent implements OnInit {

  displayedColumns: string[] = ['ProductName', 'CustomerName', 'Quantity', 
  'Rate', 'Total'];
  id;
  dataSource;


  salesTransaction: SalesTransactionElement[];
  constructor(private service: SalesTransactionsService,
          private route: ActivatedRoute) 
  { }

ngOnInit() {

this.id = this.route.snapshot.paramMap.get('id');

this.service.getSalesTransaction(this.id).subscribe((singleData: any) =>
{
  this.service.getAllSalesTransactions().subscribe((data: any) => 
  {
    data.forEach(element => 
     {

      if (singleData.CustomerName === element.CustomerName && 
      element.Status === false) {

        this.salesTransaction = element;
        console.log(this.salesTransaction);
      }

    });
  });
}

实际结果:

/ *****单独作为两个对象**** /

{SalesTranId:54,ProductId:10,CustomerId:21,InvoiceId:null,ProductName:“ Asus”}

{SalesTranId:51,ProductId:17,CustomerId:21,InvoiceId:1,ProductName:“ Dell”}

预期结果:

/ **********对象数组************ /

[{SalesTranId:54,ProductId:10,CustomerId:21,InvoiceId:null,ProductName:“ Asus”},

{SalesTranId:51,ProductId:17,CustomerId:21,InvoiceId:1,ProductName:“ Dell”}]

arrays angularjs angular typescript object
1个回答
0
投票

初始化构造函数上方的数组:salesTransaction: SalesTransactionElement[] = []

然后在forEach处理程序中推入该数组:this.salesTransaction.push(element);

© www.soinside.com 2019 - 2024. All rights reserved.