【问题标题】:Angular 5 - Dynamically add items to an arrayAngular 5 - 将项目动态添加到数组
【发布时间】:2021-02-11 16:58:39
【问题描述】:

我有这两个功能:

cart() 这是一个数组对象(此信息是静态的)

cartItems 这是一个数组对象(此信息是动态的)

cart 内可以有多个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
    );
  }

【问题讨论】:

    标签: arrays angular5 push


    【解决方案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"));
                });
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多