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

88
Vistas
Sort a json with nested objects structure and return a format same as the structure of json

My title for this ques. may not be appropriate but my problem is that I have a following type of json...

{
  "trees":{
     "name":"a",
     "age":"9",
     "height":"10",
     "location":"park"
  },
  "cars":{
     "name":"b",
     "age":"3",
     "height":"10",
     "location":"park"
  },
  "bikes":{
     "name":"c",
     "age":"110",
     "height":"10",
     "location":"park"
  }
}

Now I want to sort it according to age that is 3,9,110. And get hold of this sorted data in the same format that is

{
"cars":{
     "name":"b",
     "age":"3",
     "height":"10",
     "location":"park"
  },
  "trees":{
     "name":"a",
     "age":"9",
     "height":"10",
     "location":"park"
  },
  "bikes":{
     "name":"c",
     "age":"110",
     "height":"10",
     "location":"park"
  }
};

This format is essential for me because I pass it further to another react child component where I do several operations on it to send it further to further components.

I searched on the net and found ways to sort this array wherein firstly an array would be initialized and only the values of json would be pushed and not keys in that array and then the sorting would happen. That method works fine but the result array i get is of a different type then my original json which causes trouble is sending data further to components.

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

0

Convert to an array of key-value pairs, then sort by the value's age property converted to a number, then convert back to an object from the entries array.

Object.fromEntries(
  Object.entries(data).sort(
    ([, objA], [,objB]) => Number(objA.age) - Number(objB.age)
  )
);

const data = {
  "trees": {
    "name": "a",
    "age": "9",
    "height": "10",
    "location": "park"
  },
  "cars": {
    "name": "b",
    "age": "3",
    "height": "10",
    "location": "park"
  },
  "bikes": {
    "name": "c",
    "age": "110",
    "height": "10",
    "location": "park"
  }
}

const sortedData = Object.fromEntries(
  Object.entries(data).sort(([, objA], [,objB]) => Number(objA.age) - Number(objB.age))
);

console.log(sortedData);

about 4 years ago · Juan Pablo Isaza Denunciar

0

var data = {
  "trees":{
     "name":"a",
     "age":9,
     "height":"10",
     "location":"park"
  },
  "cars":{
     "name":"b",
     "age":3,
     "height":"10",
     "location":"park"
  },
  "bikes":{
     "name":"c",
     "age":110,
     "height":"10",
     "location":"park"
  }
},
    sorted = {};

Object
    .keys(data).sort(function(a, b){
        return data[a].age - data[b].age;
    })
    .forEach(function(key) {
        sorted[key] = data[key];
    });

    console.log(sorted);

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