I'm trying to dynamically create a JS object which has an array inside such as this one:
//other values omitted for clarity
"items": [
{
"name": "T-Shirt",
"unit_amount": {
"currency_code": "USD",
"value": "90.00"
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
},
{
"name": "Shoes",
"unit_amount": {
"currency_code": "USD",
"value": "45.00"
},
"quantity": "2",
"category": "PHYSICAL_GOODS"
}
],
I am able to create a single value with this code:
var product = {};
product.name = "T-Shirt";
product.quantity = "1";
product.category = "PHYSICAL_GOODS";
var subproduct = {};
subproduct.currency_code = "USD";
subproduct.value = "90.00";
product.unit_amount = subproduct;
var jsonString= JSON.stringify(product);
Which creates:
{
"name": "T-Shirt",
"unit_amount": {
"currency_code": "USD",
"value": "90.00"
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}
How can I add up the created values inside the array? I have an onclick event for providing the values for any given "item" in the example. For clarity, I do not know beforehand how many "items" the array will have.
itemsproperty from scratch if it doesn't exist?varand its function scope and useletandconstinstead that both have block scope.how to add an object to an array?and you will find more information when you search for it.