0

I want to add a new object for each nested array. I'm calling this function any time I add a product to my orderintake:

  add2order(productID, productName, productRatePlans) {
    this.orderIntake.push({ productID, productName, productRatePlans });
    let i = 0;
    this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
      i++;
      this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
        i
      ].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
        i
      ].defaultQuantity;
    });

  }

this is an example response from the server:

{
            "id": "8adc8f996928b9a4016929c59b943a8f",
            "sku": "SKU-00006778",
            "Partner_Account_ID__c": null,
            "productRatePlans": [
                {
                    "id": "8adce4216928c28d016929c59bff3372",
                    "status": "Active",
                    "name": "Enterprise",
                    "description": null,
                    "effectiveStartDate": "2016-02-26",
                    "effectiveEndDate": "2029-02-26",
                    "productRatePlanCharges": [
                        {
                            "id": "8adc8f996928b9a4016929c59d183a92",
                            "name": "USAGE_COUNTER_2",
                            "type": "Usage",
                            "model": "Volume",
                            "uom": "Each",
                            "pricingSummary": [
                                "Up to 5000 Each: USD0 flat fee"
                            ],
                            "pricing": [
                                {
                                    ...
                                }
                            ],
                            "defaultQuantity": null,
                            "applyDiscountTo": null,
                            "discountLevel": null,
                            "discountClass": null,
                            ...
                            "financeInformation": {
                                ..,
                            }
                        }
                    ]
                }
            ],
            "productFeatures": [
                {
                    ...
                }
            ]
        }

The data is being retrived this way from an external REST backend so unfortunately I can't initialize the data including the new property...

so in every productRatePlanCharges there should be 1 new object 'quantity'. How can I add this field to every productRatePlanCharges? Right now I'm getting: ERROR

TypeError: Cannot read property 'productRatePlanCharges' of undefined

And how can I make sure I'm always adding this to the last orderIntake element? Don't mind productRatePlans there is only 1 in each orderintake...

thanks for your support!

Malte
  • 329
  • 3
  • 14

2 Answers2

0

Here you have to create productDetails object with inititalised array like below so that you won't get the error.

add2order(productID, productName, productRatePlans) {

   // Create object like below
   let productDetails = { productID : productID, productName : productName, productRatePlans : productRatePlans 
   }   

    this.orderIntake.push(productDetails);

    let i = 0;
    this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
      i++;
      this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
        i
      ].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
        i
      ].defaultQuantity;
    });

  }

Hope this will help!

TheParam
  • 10,113
  • 4
  • 40
  • 51
  • thanks for the feedback, unfortunately the data is retrived from an external REST backend and the field is not provided out of the box. Sorry forgot to mention that. – Malte Mar 09 '19 at 05:34
  • so you can assign those array of data to productRatePlans before traversing it – TheParam Mar 09 '19 at 05:38
  • still getting OrderintakeComponent.html:188 ERROR TypeError: Cannot read property 'productRatePlanCharges' of undefined at OrderintakeComponent.push../src/app/orderintake/orderintake.component.ts.OrderintakeComponent.add2order :/ 176 is this line: this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => { – Malte Mar 09 '19 at 05:44
  • so you need to check productRatePlans array is coming properly from server or not – TheParam Mar 09 '19 at 05:47
  • it's coming I will post an example arry in my question – Malte Mar 09 '19 at 05:57
0

as you used Angular you probably use Typescript too. I recommend that you create a model like your incoming model and there define your quantity: number inside productRatePlanCharges object. then map the incoming data to your own model. therefore you will have a quantity=0 in your model that you can change it later in a loop.

If you want to continue with your own way take a look at this: Add new attribute (element) to JSON object using JavaScript

there is no problem to add an element to current model almost like you did, and the problem might be somewhere else as your error refers to existence of productRatePlanCharges!

as you used forEach I prefer to use that 'element' and double iterating with i++; is not a good idea to me. this might be better:

element.quantity = element.defaultQuantity;
Mazdak
  • 650
  • 5
  • 13