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

197
Vistas
How to push array object element in object array in jQuery

I am trying to create customized product order in this I need to store all selected options store in the JSON.

like this

let attr = [{
        fabric: 'Bargandi Check',
        style:[{
                 Jacket lapels: 'Notch'
        }],
    }];

selector image for understanding batter

I want when user select any of these options I will store in JSON when a user updates any of option it will update in the JSON object and when user select the new option it will add in JSON

like this

let attr = [{
        fabric: 'Bargandi Check',
        style:[{
                 Jacket lapels: 'Notch',
                 Lapel width: 'Slim' // this
        }],
    }];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You cannot "push" an element into a Javascript object. but you can either use the dot operator or square brackets to access an existing property / rewrite property / assign property

const obj = { name: 'ed', age: '16' };

// rewriting an existing property
obj["age"] = 20; 
obj["name"] = 'edward'; 

console.log(obj["skill"]); // will be undefined
obj["skill"] = "Javascript"; // setting it as a new property

obj["list"] = []; // the list property is an array

obj["list"].push(1); // now i can use push because its an array
console.log(obj);

One more thing, javascript properties cannot have blank spaces, but you can seperate it with a hyphen or an underscore

"An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation" - MDN

Your Corrected Object

let attr = [{
    fabric: 'Bargandi Check',
    style: [{
        Jacket_lapels: 'Notch',
        Lapel_width: 'Slim' // this
    }],
}];


const found = attr.find(x => x.fabric == 'Bargandi Check');
found.style[0].Lapel_width = 'Thick' // this will rewrite the width property to "thick"

console.log(attr);

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