Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Dynamically creating an object with an array inside

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!