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

195
Views
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 answers
Answer question

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 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!