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

122
Views
Dinamically adding objects to an existing object, and adding key/value pairs to said object

I have an object which already has 3 objects inside it. I want to dynamically add objects to one of those objects inside the original object, and then add key/value pairs to these dynamically added objects.

const result = {
    AV: {},
    Furnaces: {},
    "Production Lines": {}
};

So, this is the existing object with the objects inside. I add key/value pairs dinamically to "AV" easily because its simply

result[AV]["New key"] = value;

But if I try to run through a loop of the furnace names and add key/value pairs to the new object with that furnace's name, like so:

for (let i = 0; i < furnaces.length; i++) { 

    let furnaceName = furnace[i];

    result["Furnaces"][furnaceName]["Raw Material"] = 5;

};

it throws an error

Cannot set property "Raw Material" of undefined to "270000"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can simply use Object.assign

Assuming you have array of furnaces. Try this,

  for (let i = 0; i < furnaces.length; i++) { 

    let furnaceName = furnaces[i];
    const rawMaterial = { 'RawMaterial': 5 }
    Object.assign(result.Furnaces, {[`${furnaceName}`]: rawMaterial })

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