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

87
Views
Map array of locales and change each locale to language name

Using Next.js, I'm getting the current locale and all available locales for a language selection menu:

const {cl, al} = useContext(LangContext);
// cl = "en-US"
// al = ["en-US", "de-DE"]

I wrote this function to return the full language name:

const clAlias = ()=> {
    if (cl === "en-US") { return "English" };
    if (cl === "de-DE") { return "Deutsch" };
}

Now I want to map al , but instead of having ["en-US", "de-DE"] I want to have ["English", "Deutsch"]. It not only looks better but also makes it easier for the user to select their language. What would be the best way to do so?

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

0

How about an object.

const clAlias = {
  "en-US": "English",
  "de-DE": "Deutsch"
}
//...
al.map(x => clAlias[x])
about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.from method. It return new array without change al

And don't forget add arguments in clAlias() like clAlias(cl)

const al = ["en-US", "de-DE"];
const clAlias = (cl) => {
  if (cl === "en-US") {
    return "English";
  }
  if (cl === "de-DE") {
    return "Deutsch";
  }
};

const answer = Array.from(al, (e) => clAlias(e));
console.log(answer);

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!