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

92
Views
Ordenar jerarquía json

Tengo JSON con la siguiente estructura:

 { "name":"Scorpiones", "children":[ { "name":"Parabuthus", "children":[ { "name":"Parabuthus schlechteri" }, { "name":"Parabuthus granulatus" } ] }, { "name":"Buthidae", "children":[ { "name":"Androctonus", "children":[ { "name":"Androctonus crassicauda" }, { "name":"Androctonus bicolor" } ] } ] } ] }

Es necesario ordenar los elementos por el campo de nombre en cada nivel de la jerarquía. ¿Cómo se puede hacer esto con la ayuda de JS?

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

0

Puede usar Object.keys que devuelve una matriz que contiene todas las claves de objeto, luego puede simplemente ordenar esa matriz.

 const getSortedKeys = (obj) => { return Object.keys(obj).sort(); }

Y para lanzar todas las claves anidadas de su objeto, puede usar la recursividad:

 const trav = (obj) => { if(obj instanceof Array) { // go inside each object in the array obj.forEach(item => trav(item)) } else if(typeof obj === "object") { // sort object keys let keys = getSortedKeys(obj); // do whatevery you want... // go the the next levels keys.forEach(key => trav(obj[key])) } }

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!