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

101
Views
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 answers
Answer question

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 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!