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

168
Views
Bucle de objetos anidados en JS

Estoy tratando de agregar una serie de valores a un objeto anidado y tengo algunos problemas con el bucle en el siguiente código. Cualquier ayuda sería realmente apreciada.

 let settings = {}; function write(id, values) { if(!settings[id]) settings[id] = {}; for(var x = 0; x < Object.keys(values).length; x ++) { settings[id][values[x]] = values[values[x]]; } } //example write('example', {'prop1': 5, 'prop2': 10});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Está intentando indexar los values del objeto con x , que es un número. Para recorrer las claves de su objeto, puede usar un bucle for...in :

 function write(id, values) { if(!settings[id]) settings[id] = {}; for(const key in values) { settings[id][key] = values[key]; } }

Otro enfoque sería utilizar la desestructuración de objetos:

 function write(id, values) { settings[id] = { ...(settings[id] || {}), ...values }; }
about 4 years ago · Juan Pablo Isaza Report

0

values es un objeto. Acceder a values[x] devolverá indefinido.

Debe acceder a él con las claves correctas en ese objeto como se muestra a continuación.

 let settings = {}; function write(id, values) { if (!settings[id]) settings[id] = {}; const keys = Object.keys(values); for (var x = 0; x < keys.length; x++) { settings[id][keys[x]] = values[keys[x]]; } console.log(settings) } //example write('example', { 'prop1': 5, 'prop2': 10 });

about 4 years ago · Juan Pablo Isaza Report

0

intente mantener el retorno de Object.keys (valores) en otra variable y utilícelo para asignar valor en una configuración como esta

 function write(id, values) { if(!settings[id]) settings[id] = {}; const key = Object.keys(values) for(var x = 0; x < key.length; x ++) { settings[id][key[x]] = values[key[x]]; } }
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!