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

114
Views
Ejecutar una función recursivamente en Javascript

Creé esta función:

 const demoD = ( obj, toChange, newV, ) => { return Object.keys(obj).reduce((acc, key) => { if (typeof obj[key] === 'object') { demoD(obj[key], toChange, newV); } acc[key] = obj[key] === toChange ? newV : obj[key] return acc }, {}) } const obj = { name: '', age: '687', demo: { test: '' } } console.log(demoD(obj, '', null))

La idea de esta función es cambiar un valor del objeto por uno nuevo, y debe ejecutarse recursivamente.
Espero este resultado:

 const obj = { name: null', age: '687', demo: { test: null' } }

Pero no obtengo este resultado, y la test sigue sin cambios.
Pregunta: ¿Cómo solucionarlo y cuál es el problema?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si desea aplicar cambios al objeto original en su lugar:

 const demoD = ( obj, toChange, newV, ) => { return Object.keys(obj).reduce((acc, key) => { if (typeof obj[key] === 'object') { demoD(obj[key], toChange, newV); } acc[key] = obj[key] === toChange ? newV : obj[key] return acc }, obj) // 👈 send in obj here }

Si no desea mutar su objeto original, entonces debería ser:

 const demoD = ( obj, toChange, newV, ) => { return Object.keys(obj).reduce((acc, key) => { if (typeof obj[key] === 'object') { acc[key] = demoD(obj[key], toChange, newV) } else { acc[key] = obj[key] === toChange ? newV : obj[key] } return acc }, {}) }
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!