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

108
Vistas
Sort deeply nested objects alphabetically by keys

We are using vue-i18n and maintain our messages within an js variable. This leads to deeply nested objects or key/value pairs.

const messages = {
  en: {
    message: {
      page1: {
        title: "Some title",
        button: {
          title: "Foo",
        },
        subpage: {
          ...
        }
      },
      ...
    },
  },
  de: {...},
};

As u can see, without an appropriate sorting this file will be really confusing. My idea is to sort the whole file alphabetically by keys.

Is there an algorithm/code that can be used for this? Or do I have to write it myself?

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

0

You can do some recursivity like :

I used the following answer to write the order function

const order = (unordered) => Object.keys(unordered).sort().reduce(
  (obj, key) => {
    obj[key] = unordered[key];
    return obj;
  }, {}
);

const message = {
  fr: {
    message: "Bonjour",
    a: 1
  },
  en: {
    message: "Hello",
    a: {
      c: 1,
      b: 2
    }
  },
  es: "Hola"
}

const sortObjectDeeply = (object) => {
  for (let [key, value] of Object.entries(object)) {
    if (typeof(value) === "object") {
      object[key] = sortObjectDeeply(value)
    }
  }
  return order(object)
}

console.log(sortObjectDeeply(message))

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