Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
How to sort through complex object by key

I have been struggling to find a helpful resource given the peculiar nature of my object... I would like to simply sort an object alphabetically however I need to sort by my key.

here is an example object with the same schema of the object I am working with

{
     "sections": [
         {
           "title": "21Q4 Results",
           "states": {
                        "Maine": {
                                   "string1":[{stuff1}],
                                   "string2":[{stuff2}],                
                                 },
                        "Arkansas": {
                                   "string1":[{stuff1}],
                                   "string2":[{stuff2}],                
                                 },
                        "Kentucky": {
                                   "string1":[{stuff1}],
                                   "string2":[{stuff2}],                
                                 },
                      }
         }
                 ]
}

The goal is to arrange the sections.states in alphabetical order. Unfortunately, the structure of this object is generated by an API which I have no control over, so changing the structure from the backend is not really an option.

What is the best way to achieve this? Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is a way to convert the states object into a sorted array in order to preserve a sort order.

let stuff1 = stuff2 = 'foobar'
let data = {
  "sections": [{
    "title": "21Q4 Results",
    "states": {
      "Maine": {
        "string1": [{ stuff1 }], "string2": [{ stuff2 }],
      },
      "Arkansas": {
        "string1": [{ stuff1 }], "string2": [{ stuff2 }],
      },
      "Kentucky": {
        "string1": [{ stuff1 }], "string2": [{ stuff2 }],
      },
    }
  }]
}

let newstates = Object.keys(data.sections[0].states).sort((a, b) => a.localeCompare(b)).map(s => ({state: s, data: data.sections[0].states[s] }));
console.log(newstates)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda