I need to produce an array formatted as :
products :
[
{
"product_id": 32,
"quantity": 2
},
{
"product_id": 33,
"quantity": 2,
"product_options":
[
{
"id": 87,
"value": 10
}
]
}
]
I can do the product_id & quantity ok using :
productID = 32;
prodQuantity = 2;
var row2 = {};
row2.product_id = productID;
row2.quantity = prodQuantity;
product.push(row2);
productID = 33;
prodQuantity = 2;
var row2 = {};
row2.product_id = productID;
row2.quantity = prodQuantity;
product.push(row2);
How do I add the element product_options with the id & value.
I have tried variations of:
var row3 = {};
row3.id = 87;
row3.value = 10;
cartArray['product_options'].push(row3);
I managed it using:
var prodQuantity = app.getValue('popupDropdown').value;
var cartArray = [];
var optionID = app.getValue('popupDropdown4').value;
var row2 = {};
var row3 = {};
var row4 = {};
var tempArray = [];
row2.product_id = productID;
row2.quantity = prodQuantity;
cartArray.push(row2);
row3.id = optionID;
row3.value = Rule4Value;
tempArray.push(row3);
row4.product_options = tempArray;
cartArray.push(row4);
But I see the answer below is much simpler.