Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Javascript 使用else推送数组中的项_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 使用else推送数组中的项

Javascript 使用else推送数组中的项,javascript,angular,typescript,Javascript,Angular,Typescript,在我的typescript代码中,我有一个产品数组 products = [10,25,45,89]; 我正在打一个服务电话来检索购买该产品的客户 products.forEach(x => this.clientService.getclientOrdersByProduct(clientId, x) .subscibe(s=>{ if (s){ this.clientOrders.push(s); } else{

在我的typescript代码中,我有一个产品数组

products = [10,25,45,89];
我正在打一个服务电话来检索购买该产品的客户

products.forEach(x => 
  this.clientService.getclientOrdersByProduct(clientId, x)
  .subscibe(s=>{ 
     if (s){ 
       this.clientOrders.push(s);
     } else{ 
       // add the product id
     }
   })
); 
问题是我想在clientOrders中添加一个else并推送产品Id。ClientOrders类是这样的

export interface ClientAccount{  product?: Product; ....}
我怎样才能在其他方面做到这一点


谢谢

假设界面是这样的:

export interface ClientAccount{  
  product?: Product;
  productId: number;
  ...
}
您可以这样做:

创建clientOrder的数组:

clientOrder: ClientAccount[] = [];
为了


有些内容用粗体表示,因为我不知道您从该订阅中获得的数据是什么。

我不知道您在问什么。你说添加一个else是什么意思?请在你的问题中包含更多的信息,你问的不清楚。我已经添加了关于我希望else在哪里的评论。你是说s{foo;}else{push into clientOrders}?@JacopoSciampi感谢它正在工作,如果你将它添加到解决方案中,我会标记为答案
products.forEach(x => 
  this.clientService.getclientOrdersByProduct(clientId, x)
  .subscibe(s=>{ 
     if (s){ 
       this.clientOrders.push(s);
     } else{ 
       this.clientOrder.push({product: **something**, productId: x);
     }
   })
);