arrays - Angular 5 - 将项目动态添加到数组
问题描述
我有这两个功能:
cart()是一个数组对象(此信息是静态的)
cartItems是一个数组对象(此信息是动态的)
在购物车内可以有几个cartItems。如何将不同的物品推入此购物车,以便最终结果如下所示:
JSON:
"cart": [
{
"id": "1",
"cartItem": [
{
"id": "cart Item 1",
"action": "New",
"quantity": 1,
},
{
"id": "cart Item 2",
"action": "New",
"quantity": 1,
}
}
]
功能:
private cart(): CaCartItemsModel{
return new CaCartItemsModel(
"1",
"Cambio",
"TV"
);
private cartItems(): CaCartItemModel{
return new CaCartItemModel(
"cart Item 1/cart Item 2",
"New",
1
);
}
解决方案
我设法通过以下方式解决了我需要的问题:
private createCartItems(cancellationModel: CancellationModel) {
this.request.caAcProductOrderModel.caCartItems.getAllCartItems()
.push(this.setCartItems());
cancellationModel.channels.forEach(channel => {
this.request.caAcProductOrderModel.caCartItems.getAllCartItems()
.forEach(caCartItem => {
caCartItem.caCartItem.getAllCartItem()
.push(this.setCaCartItemModel(
channel.id,
channel.action,
channel.name,
"",
true
));
caCartItem.caCartItem.getAllCartItem()
.forEach(cartItem => {
cartItem.caProduct.caProductRelationship.getAllCaProductRelationship()
.push(this.setCaProductRelationshipModel(channel.type));
cartItem.caProductOffering.caCategory.getAllCategories()
.push(this.setCaCategoryModel("promo"));
cartItem.caProductOffering.caAttributes.getAllAttributes()
.push(this.setCaAttributesModel("Numero fijo Asociado", this.activeCustomerServiceProvider.getPhoneNumber()));
cartItem.cartItemRelationship.getAllCartItemRelationship()
.push(this.setCaCartItemRelationshipModel(channel.id, "reliesOn"));
});
});
});
}
推荐阅读
- mongodb - AWS DocumentDB 中架构验证的替代方法
- css - 如何防止背景图像像素化?
- sql-server - SSIS -- 未能部署项目。错误 27203
- python - 根据条件生成新列
- javascript - Angular Router usehash:导航不保留hastag之前的url
- html - 如何在使用 puppeteer 生成的 pdf 上使用 Roboto 字体系列
- swift - 在 List SwiftUI 2.0 中的 TextField 之间切换焦点
- angular - 使用@angular/cdk/drag-drop 中的 cdkDragFreeDragPosition 平滑移动到 x 和 y 位置
- react-native - 在您的分支中找不到任何 React Native Android 项目 - 代码推送
- python - numpy float32 转换中的精度损失