Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda