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

127
Views
Cómo cambiar todos los nombres de las claves dentro de una matriz anidada de objetos javascript

He anidado una matriz de objetos como a continuación:

 const data = [ {text: 'node 1'}, {text: 'node 2', chapter_playlist: [{text: 'node 2-1', lesson_playlist: [{text: 'node 3-1'}]}]}, {text: 'node 3'}, {text: 'node 4', chapter_playlist: [{ text: 'node 4-1' }]} ]

¿Cómo cambiar el nombre de cada propiedad anidada como chapter_playlist , lección_playlist a ' niños '?

básicamente quiero cambiar el nombre de la propiedad que tiene más niños, si no hay niños entonces no hay necesidad de cambiarlo. Y todavía estoy confundido sobre cómo cambiarlo.

Resultados previstos

 const data = [ {text: 'node 1'}, {text: 'node 2', children: [{text: 'node 2-1', children: [{text: 'node 3-1'}]}]}, {text: 'node 3'}, {text: 'node 4', children: [{ text: 'node 4-1' }]} ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dato curioso: si necesita caminar a través de un objeto por cualquier motivo, JSON.stringify() es su amigo.

No porque desee convertir sus datos en una cadena JSON, sino porque también es un iterador de objetos que le permite realizar un procesamiento arbitrario en todos los niveles mediante una función de reemplazo:

 const data = [ {text: 'node 1'}, {text: 'node 2', chapter_playlist: [{text: 'node 2-1', lesson_playlist: [{text: 'node 3-1'}]}]}, {text: 'node 3'}, {text: 'node 4', chapter_playlist: [{ text: 'node 4-1' }]} ] const rewriteList = [`chapter_playlist`, `lesson_playlist`]; function replacer(key, value) { // If the value at this depth is an object (but not an iterable // like array or Set or Map), then rebind the properties you // need rebound, provided they exist: if (typeof value === `object` && !value[Symbol.iterator]) { rewriteList.forEach(prop => { if (value[prop]) { // By editing "value", we're directly updating "data". value.children = value[prop]; delete value[prop]; } }); } return value; } JSON.stringify(data, replacer); console.log(data)

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!