1

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.

5
  • 1
    Have a look at this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 16, 2021 at 7:17
  • When you say "I am trying to dynamically create an object" do you mean: 1. you want to push object to the array or 2. create a whole main object with the items property from scratch if it doesn't exist? Commented Aug 16, 2021 at 7:29
  • 1
    Do yourself a favor and skip var and its function scope and use let and const instead that both have block scope. Commented Aug 16, 2021 at 7:29
  • 1
    Your question should be how to add an object to an array? and you will find more information when you search for it. Commented Aug 16, 2021 at 7:30
  • @ross-u I meant to create a new object based on the second code block and push it to the array, the omitted "main" object values are static and do not need to be dynamically created. Commented Aug 16, 2021 at 7:37

2 Answers 2

1

To add the object to an array you should use the array method .push().

You could do it in the following way:

// Object which has a property `items`, where we will store product objects
var main = {
  items: []
};

// Create the full product object
var product = {
  name: "T-Shirt";
  quantity: "1";
  category: "PHYSICAL_GOODS";
  unit_amount: {
    currency_code = "USD";
    value = "90.00";
  }
};

// Push the new object to the `items` array
main.items.push(product);

Sign up to request clarification or add additional context in comments.

1 Comment

@Esquirish I understood that you are using an onclick event listener to add a product to the array. I was not sure how you go about this, so I simplified the example.
1

You are on the right path, just iterate your code and put it in an array :

var productList = [];

for (var i = 0 ; i < 2; i++) {
  // your 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;

  productList.push(product);
}

var answer = JSON.stringify(productList);
console.log(answer);

2 Comments

Thank you for your insight, much appreciated
You are not required to iterate in order to push the item to an array. You should omit the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.