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

125
Views
Cambiar recursivamente el nombre de la propiedad anidada del objeto

Tengo un objeto con campos anidados que subelemento con nombre. Necesito cambiar los nombres de este campo a los niños.

¿Cómo se puede implementar ordenadamente esta funcionalidad? ¿Quizás hay formas además de la recursividad?

 let data = [ { name: 'Alex', subItem: [ { name: 'Den', subItem: [ ] } ] } ] function transformData = (data) => { return data.map(stat=>{ }) } console.log(transformData(data))

//debería ser

 let data = [ { name: 'Alex', children: [ { name: 'Den', children: [] } ] } ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Javascript ya incluye un transformador de objetos recursivo, JSON. stringify con función de replacer :

 let data = [ { name: 'Alex', subItem: [ { name: 'Den', subItem: [ {name: 'Foo', subItem: []} ], } ] } ] JSON.stringify(data, function(_, v) { if (v.subItem) { v.children = v.subItem delete v.subItem } return v }) console.log(data)

En el caso simple como este, también puede simplemente manipular la cadena JSON directamente:

 newData = JSON.parse( JSON.stringify(data) .replaceAll(`"subItem":`, `"children":`))
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!