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

123
Views
Agregar dinámicamente objetos a un objeto existente y agregar pares clave/valor a dicho objeto

Tengo un objeto que ya tiene 3 objetos dentro. Quiero agregar dinámicamente objetos a uno de esos objetos dentro del objeto original y luego agregar pares clave/valor a estos objetos agregados dinámicamente.

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

Entonces, este es el objeto existente con los objetos dentro. Agrego pares clave/valor dinámicamente a "AV" fácilmente porque es simplemente

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

Pero si trato de ejecutar un ciclo de los nombres de los hornos y agregar pares clave/valor al nuevo objeto con el nombre de ese horno, así:

 for (let i = 0; i < furnaces.length; i++) { let furnaceName = furnace[i]; result["Furnaces"][furnaceName]["Raw Material"] = 5; };

arroja un error

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

0

simplemente puede usar Object.assign

Suponiendo que tiene variedad de hornos. Prueba esto,

 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!